b_94_Binary_Tree_Inorder_Traversal

//
// Created by Mr.Hu on 2018/10/23.
//
// leetcode 94 binary tree inorder traserval
//
// 对二叉树进行中序遍历的 递归方法 和 循环方法
//
// 循环方法主要借助栈数据结构,每次先将节点右子节点放入栈中,并将当前节点右指针赋为nullptr,然后将当前节点放入栈中,
// 再将当前节点左子节点放入栈中并将其左指针赋值为nullptr。以此来防止一个节点的左右节点多次访问。
//

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#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 inorder(TreeNode *root, vector<int> &result) {
if (root != nullptr) {
if (root->left != nullptr) inorder(root->left, result);
result.push_back(root->val);
if (root->right != nullptr) inorder(root->right, result);
}
}

// recusive solution
vector<int> inorderTraversal_recursive(TreeNode *root) {
vector<int> result;
inorder(root, result);
return result;
}

// iterative solution
vector<int> inorderTraversal_iterative(TreeNode *root) {
vector<int> result;
if (root == nullptr) {
return result;
}
stack<TreeNode *> cur_nodes;
if (root->right != nullptr) {
cur_nodes.push(root->right);
root->right = nullptr;
}
cur_nodes.push(root);
if (root->left != nullptr) {
cur_nodes.push(root->left);
root->left = nullptr;
}
while (!cur_nodes.empty()) {
TreeNode *cur = cur_nodes.top();
cur_nodes.pop();
if (cur->left == nullptr && cur->right == nullptr) {
result.push_back(cur->val);
continue;
}
if (cur->right != nullptr) {
cur_nodes.push(cur->right);
cur->right = nullptr;
}
cur_nodes.push(cur);
if (cur->left != nullptr) {
cur_nodes.push(cur->left);
cur->left = nullptr;
}
}
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.inorderTraversal_recursive(&root);
vector<int> result = solution.inorderTraversal_iterative(&root);
for (auto it:result) {
cout << it << " ";
}
return 0;
}