-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution144.hpp
More file actions
60 lines (52 loc) · 1.47 KB
/
Solution144.hpp
File metadata and controls
60 lines (52 loc) · 1.47 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
57
58
59
60
//
// Solution144.hpp
// Algorithm
//
// Created by Pancf on 2019/12/21.
// Copyright © 2019 Pancf. All rights reserved.
//
#ifndef Solution144_hpp
#define Solution144_hpp
#include <stdio.h>
#include <vector>
using std::vector;
class Solution144 {
/**
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
================================================================================================
Accept details:
Runtime: 8 ms, faster than 59.97% of C++ online submissions for Binary Tree Preorder Traversal.
Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Binary Tree Preorder Traversal.
思路:递归解法借用了函数调用栈的特性,要使用迭代解法的话自己维护一个遍历栈即可。
*/
public:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
vector<int> preorderTraversal(TreeNode* root);
static void test() {
TreeNode root{1};
TreeNode node1{2};
TreeNode node2{3};
root.right = &node1;
node1.left = &node2;
Solution144 s = Solution144();
auto rv = s.preorderTraversal(&root);
for (auto ele : rv) {
printf("%d ", ele);
}
}
};
#endif /* Solution144_hpp */