c_144_Binary_Tree_Preorder_Traversal

//
// Created by Mr.Hu on 2018/10/23.
//
// leetcode 144 binary tree preorder traversal
//
// 二叉树先序遍历
//
// 递归方法 & 循环实现
//

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
61
62
63
64
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;

TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
void preorder(TreeNode *root, vector<int> &result) {
if (root != nullptr) {
result.push_back(root->val);
if (root->left != nullptr) preorder(root->left, result);
if (root->right != nullptr) preorder(root->right, result);
}
}

// recursive solution
vector<int> preorderTraversal_recursive(TreeNode *root) {
vector<int> result;
preorder(root, result);
return result;
}

// iterative solution
vector<int> preorderTraversal_iterative(TreeNode* root) {
vector<int> result;
stack<TreeNode*> cur_nodes;
if(root==nullptr){
return result;
}
cur_nodes.push(root);
while(!cur_nodes.empty()){
TreeNode* cur = cur_nodes.top();
cur_nodes.pop();
result.push_back(cur->val);
if(cur->right!=nullptr) cur_nodes.push(cur->right);
if(cur->left!=nullptr) cur_nodes.push(cur->left);
}
return result;
}
};

int main() {
TreeNode root(1);
TreeNode a(2);
TreeNode b(3);
root.right = &a;
a.left = &b;
Solution solution;
// vector<int> result = solution.preorderTraversal_recursive(&root);
vector<int> result = solution.preorderTraversal_iterative(&root);
for (auto it:result) {
cout << it << " ";
}
return 0;
}