-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeInorderTraversal.h
113 lines (93 loc) · 3.32 KB
/
BinaryTreeInorderTraversal.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Author: naiyong, aonaiyong@gmail.com
Date: Sep 28, 2014
Problem: Binary Tree In-order Traversal
Difficulty: 4
Source: https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
Notes:
Given a binary tree, return the in-order traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
Solution: http://en.wikipedia.org/wiki/Tree_traversal
1. Recursive In-order Traversal.
Time: O(n) (to be exact, 2n)
Space: O(n) (left-skewed binary tree)
2. Iterative In-order Traversal (Stack).
Time: O(n) (to be exact, 2n)
Space: O(n) (left-skewed binary tree)
3. Morris In-order Traversal.
Time: O(n), Space: O(1)
Note that we have to fully traverse the tree to recover it.
For 1&2, each edge is traversed exactly once.
For 3, each edge is traversed at most 3 times.
*/
#ifndef BINARYTREEINORDERTRAVERSAL_H_
#define BINARYTREEINORDERTRAVERSAL_H_
#include <vector>
using std::vector;
#include <stack>
using std::stack;
#include "TreeNode.h"
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> values;
morrisInorder(root, values);
return values;
}
void recursiveInorder(TreeNode *root, vector<int> &values) {
if (!root)
return;
recursiveInorder(root->left, values);
values.push_back(root->val);
recursiveInorder(root->right, values);
}
void iterativeInorder(TreeNode *root, vector<int> &values) {
stack<TreeNode *> stk;
TreeNode *node = root;
while (node || !stk.empty()) {
if (node) {
stk.push(node);
node = node->left;
}
else {
node = stk.top();
stk.pop();
values.push_back(node->val);
node = node->right;
}
}
}
void morrisInorder(TreeNode *root, vector<int> &vals) {
TreeNode *node = root;
while (node) {
if (!node->left) { // left subtree is empty
vals.push_back(node->val); // process node
node = node->right; // advance to right subtree
}
else {
// find the rightmost node of the left subtree
TreeNode *rightMost = node->left;
while (rightMost->right && rightMost->right != node)
rightMost = rightMost->right;
if (!rightMost->right) { // left subtree is to be processed
rightMost->right = node; // make node rightMost's right child
node = node->left; // advance to left subtree
}
else { // left subtree is finished
vals.push_back(node->val); // process node
rightMost->right = nullptr; // restore rightMost's right child to nullptr
node = node->right; // advance to right subtree
}
}
}
}
};
#endif /* BINARYTREEINORDERTRAVERSAL_H_ */