leetcode题解-94-二叉树的中序遍历

题目

My way

递归实现

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int> vi;
void helper(TreeNode* root)
{
if (!root) return ;
helper(root->left);
vi.push_back(root->val);
helper(root->right);
}

public:
vector<int> inorderTraversal(TreeNode* root) {
helper(root);

return vi;
}
};

手动压栈实现

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
vector<int> vi;

public:
vector<int> inorderTraversal(TreeNode* root)
{
if (root)
{
stack<TreeNode*> s;

while(!s.empty() || root)
{
if (root)
{
s.push(root);
root = root->left;
}
else
{
root = s.top(); s.pop();
vi.push_back(root->val);
root = root->right;
}
}

}

return vi;
}
};

总结

  • 因为中序遍历是 "左根右",所以得
    • 先遍历到最左的结点,则这个结点可能是 "左根右"中的 或者 或者 null
    • 若为 /, 则 都需保存它的右孩子(为/不空),然后继续考察他的 ”左根右“结构。
    • 不为null, 则 保存当前结点,且继续往左孩子遍历
    • 为null,则 弹栈 并 打印当前栈顶元素值,然后往右孩子遍历