-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path144-binary-tree-preorder-traversal.cpp
More file actions
56 lines (51 loc) · 1.36 KB
/
Copy path144-binary-tree-preorder-traversal.cpp
File metadata and controls
56 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 144. Binary Tree Preorder Traversal
//
// Given a binary tree, return the preorder traversal of its nodes' values.
//
// For example:
// Given binary tree {1,#,2,3},
// 1
// \
// 2
// /
// 3
// return [1,2,3].
// Note: Recursive solution is trivial, could you do it iteratively?
//
// Tags: Tree, Stack
//
// https://leetcode.com/problems/binary-tree-preorder-traversal/
#include <iostream>
#include <gtest/gtest.h>
#include <tree/tree.h>
#include <stack>
using namespace std;
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode *>s;
if(root == NULL) return result;
s.push(root);
while(!s.empty()){
TreeNode *n = s.top();
s.pop();
result.push_back(n->val);
if(n->right) s.push(n->right);
if(n->left) s.push(n->left);
}
return result;
}
};
TEST(leetcode_144_binary_tree_preorder_traversal, Basic)
{
Solution *solution = new Solution();
vector<int> excepted = {3, 1, 2};
EXPECT_EQ(excepted, solution->preorderTraversal(tree_init({"3", "1", "2"})));
excepted = {1, 4, 2, 3};
EXPECT_EQ(excepted, solution->preorderTraversal(tree_init({"1", "4", "3", "2"})));
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}