Skip to content

Commit ca2eb3e

Browse files
committed
Binary Tree Preorder Traversal
1 parent 5e60688 commit ca2eb3e

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
+ [136 Single Number(位运算)](algorithms/SingleNumber)
5353
+ [141 Linked List Cycle(快慢指针法)](algorithms/LinkedListCycle)
5454
+ [142 Linked List Cycle II](algorithms/LinkedListCycle2)
55+
+ [144 Binary Tree Preorder Traversal(迭代法前序遍历](algorithms/BinaryTreePreorderTraversal)
5556
+ [146 LRU Cache(空间换时间)](algorithms/LRUCache)
5657
+ [147 Insertion Sort List(链表操作,插入排序)](algorithms/InsertionSortList)
5758
+ [150 Evaluate Reverse Polish Notation(栈的应用)](algorithms/EvaluateReversePolishNotation)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Binary Tree Preorder Traversal
2+
3+
Given a binary tree, return the preorder traversal of its nodes' values.
4+
5+
For example:
6+
Given binary tree {1,#,2,3},
7+
```
8+
1
9+
\
10+
2
11+
/
12+
3
13+
```
14+
15+
return `[1,2,3]`.
16+
17+
Note: Recursive solution is trivial, could you do it iteratively?
18+
19+
## Solution
20+
21+
前序遍历,使用递归,就太简单了。
22+
23+
如何使用迭代的方式,其实就是使用栈模拟递归(注意递归(DFS)和栈的关系,队列和BFS的关系)
24+
25+
先访问节点,然后压入右孩子,压入左孩子.
26+
```cpp
27+
vector<int> preorderTraversal(TreeNode *root) {
28+
vector<int> result;
29+
if (root == NULL)
30+
return result;
31+
stack<TreeNode *>s;
32+
s.push(root);
33+
while (!s.empty()) {
34+
TreeNode *p = s.top();
35+
s.pop();
36+
result.push_back(p->val);
37+
if (p->right)
38+
s.push(p->right);
39+
if (p->left)
40+
s.push(p->left);
41+
}
42+
return result;
43+
}
44+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <string>
4+
#include <algorithm>
5+
#include <cstdio>
6+
#include <stack>
7+
using namespace std;
8+
struct TreeNode {
9+
int val;
10+
TreeNode *left;
11+
TreeNode *right;
12+
TreeNode(int x) : val(x), left(nullptr), right(nullptr){}
13+
};
14+
class Solution {
15+
public:
16+
vector<int> preorderTraversal(TreeNode *root) {
17+
vector<int> result;
18+
if (root == NULL)
19+
return result;
20+
stack<TreeNode *>s;
21+
s.push(root);
22+
while (!s.empty()) {
23+
TreeNode *p = s.top();
24+
s.pop();
25+
result.push_back(p->val);
26+
if (p->right)
27+
s.push(p->right);
28+
if (p->left)
29+
s.push(p->left);
30+
}
31+
return result;
32+
}
33+
};
34+
TreeNode *mk_node(int val)
35+
{
36+
return new TreeNode(val);
37+
}
38+
TreeNode *mk_child(TreeNode *root, TreeNode *left, TreeNode *right)
39+
{
40+
root->left = left;
41+
root->right = right;
42+
return root;
43+
}
44+
TreeNode *mk_child(TreeNode *root, int left, int right)
45+
{
46+
return mk_child(root, new TreeNode(left), new TreeNode(right));
47+
}
48+
TreeNode *mk_child(int root, int left, int right)
49+
{
50+
return mk_child(new TreeNode(root), new TreeNode(left), new TreeNode(right));
51+
}
52+
int main(int argc, char **argv)
53+
{
54+
Solution solution;
55+
TreeNode *root = mk_child(10,5,15);
56+
mk_child(root->right, 6, 20);
57+
mk_child(root->left, 2, 3);
58+
auto result = solution.preorderTraversal(root);
59+
for_each(result.begin(), result.end(), [](int i){cout << i << " ";});
60+
cout << endl;
61+
return 0;
62+
}

0 commit comments

Comments
 (0)