-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path094_BinaryTreeInorderTraversal94.java
89 lines (71 loc) · 2.01 KB
/
094_BinaryTreeInorderTraversal94.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
/**
* Given a binary tree, return the inorder traversal of its nodes' values.
*
* For example:
* Given binary tree [1,null,2,3],
* 1
* \
* 2
* /
* 3
* return [1,3,2].
*
* Note: Recursive solution is trivial, could you do it iteratively?
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class BinaryTreeInorderTraversal94 {
public List<Integer> inorderTraversal(TreeNode root) {
if (node == null) {
return new ArrayList<Integer>();
}
List<Integer> result = new ArrayList<>();
if (root.left == null && root.right == null) {
result.add(root.val);
return result;
}
result.addAll(inorderTraversal(root.left));
result.add(root.val);
result.addAll(inorderTraversal(root.right));
return result;
}
/**
* https://discuss.leetcode.com/topic/6478/iterative-solution-in-java-simple-and-readable
*/
public List<Integer> inorderTraversal2(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while (cur != null || !stack.empty()){
while (cur != null){
stack.add(cur);
cur = cur.left;
}
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
return list;
}
public List<Integer> inorderTraversal3(TreeNode root) {
List<Integer> res = new ArrayList<>();
inorderTraversal(root, res);
return res;
}
private void inorderTraversal(TreeNode root, List<Integer> res) {
if (root == null) return;
inorderTraversal(root.left, res);
res.add(root.val);
inorderTraversal(root.right, res);
}
}