-
Notifications
You must be signed in to change notification settings - Fork 22
/
94. Binary Tree Inorder Traversal.java
executable file
·110 lines (89 loc) · 2.55 KB
/
94. Binary Tree Inorder Traversal.java
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
E
tags: Hash Table, Stack, Tree
time: O(n)
space: O(logn)
Inorder traverse Binary Tree
#### Method1: DFS
- option1: dfs + rst list to carry results
- option2: Divide and Conquer, 在自己的基础上recursive, 不用helper function
- O(n) time
#### Method2: Iterative, Stack inorder traversal
- 1) Add root.leftPath all the way to leaf, 2) process curr 3) Move to right if applicable 4) add all right.leftPath
- O(n) time, O(h) space
```
/*
Given a binary tree, return the inorder traversal of its nodes' values.
Example
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Challenge
Can you do it without recursion?
Tags Expand
Recursion Binary Tree Binary Tree Traversal
*/
// Method1 option1: DFS with helper
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> rst = new ArrayList<Integer>();
dfs(rst, root);
return rst;
}
public void dfs(List<Integer> rst, TreeNode node) {
if (node == null) return;
dfs(rst, node.left);
rst.add(node.val);
dfs(rst, node.right);
}
}
/*
// Method1 Option2: DFS w/o helper
Recursive, append left, itself, then right
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> rst = new ArrayList<>();
if (root == null) return rst;
List<Integer> left = inorderTraversal(root.left);
List<Integer> right = inorderTraversal(root.right);
rst.addAll(left);
rst.add(root.val);
rst.addAll(right);
return rst;
}
}
/*
Method2: Iterative, Stack, always treat left-most-leaf with priority
- add node.left till end.
- consume stack.pop()
- if has right, add node.right and push all node.right's left children to stack
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> rst = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
addLeftPath(stack, node); // Dive deep to left path till leaf
while (!stack.isEmpty()) {
node = stack.pop(); // Add to rst
rst.add(node.val);
// Add node.right and all its left children
if (node.right != null) {
node = node.right;
addLeftPath(stack, node);
}
}
return rst;
}
private void addLeftPath(Stack<TreeNode> stack, TreeNode node) {
while (node != null) {
stack.push(node);
node = node.left;
}
}
}
```