-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP94.java
More file actions
162 lines (138 loc) · 4.88 KB
/
P94.java
File metadata and controls
162 lines (138 loc) · 4.88 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
94. 二叉树的中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/32/trees-and-graphs/85/
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,3,2]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
*/
import java.util.*;
public class P94{
public static void main(String[] args) {
TreeNode root = stringToTreeNode("[1,2,3,4,5,6,7,8]");
prettyPrintTree(root);
System.out.println(new Solution().inorderTraversal(root));
System.out.println(new Solution().inorderTraversal_iter(root));
}
static class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderTraversal_r(root, result);
return result;
}
// 递归方式
public void inorderTraversal_r(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
// 中序, 根节点在中间的顺序
inorderTraversal_r(root.left, list);
list.add(root.val);
inorderTraversal_r(root.right, list);
}
// 迭代方式
// 使用栈
public List<Integer> inorderTraversal_iter(TreeNode root) {
List<Integer> result = new ArrayList<>();
LinkedList<TreeNode> stack = new LinkedList<>();
// 将所有的左子树先入栈
while (root != null) {
stack.push(root);
//System.out.println("push left: " + stack);
root = root.left;
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
result.add(node.val);
if (node.right == null) {
// 每pop一次检查:右边为空就一直pop
node = stack.pop();
//System.out.println(" pop" + stack);
while (!stack.isEmpty() && stack.peek().right == node) {
node = stack.pop();
//System.out.println(" pop" + stack);
}
} else {
// 不为空就去右边,然后再一次将左边的一条下来全加进stack里
node = node.right;
while (node != null) {
stack.push(node);
//System.out.println("push" + stack);
node = node.left;
}
}
}
return result;
}
}
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
@Override
public String toString() {
return val+"";
}
}
public static TreeNode stringToTreeNode(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return null;
}
String[] parts = input.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
int index = 1;
while(!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();
if (index == parts.length) {
break;
}
item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int leftNumber = Integer.parseInt(item);
node.left = new TreeNode(leftNumber);
nodeQueue.add(node.left);
}
if (index == parts.length) {
break;
}
item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int rightNumber = Integer.parseInt(item);
node.right = new TreeNode(rightNumber);
nodeQueue.add(node.right);
}
}
return root;
}
public static void prettyPrintTree(TreeNode node, String prefix, boolean isLeft) {
if (node == null) {
System.out.println("Empty tree");
return;
}
if (node.right != null) {
prettyPrintTree(node.right, prefix + (isLeft ? "│ " : " "), false);
}
System.out.println(prefix + (isLeft ? "└── " : "┌── ") + node.val);
if (node.left != null) {
prettyPrintTree(node.left, prefix + (isLeft ? " " : "│ "), true);
}
}
public static void prettyPrintTree(TreeNode node) {
prettyPrintTree(node, "", true);
}
}