-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
181 lines (169 loc) · 5.41 KB
/
BinaryTree.java
File metadata and controls
181 lines (169 loc) · 5.41 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package Algorithm.Tree;
import java.lang.reflect.Array;
import java.util.ArrayDeque;
import java.util.ArrayList;
/**
* Created by lenovo on 2017/4/1.
*/
public class BinaryTree {
public TreeNode root;
public void visit(TreeNode node) {
System.out.print(node.val + ",");
}
public BinaryTree(TreeNode root) {
this.root = root;
}
public TreeNode init(String data) {
//将字符串形如"1,2,3,#,#,4,5"转化为二叉树,即反序列化,#代表空节点
if (data == null)
return null;
String[] vals = data.split(",");
ArrayList<TreeNode> queue = new ArrayList<>();
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
queue.add(root);
int index = 0;//index in queue
boolean isLeftChild = true;
for (int i = 1; i < vals.length; i++) {
if (!vals[i].equals("#")) {
TreeNode node = new TreeNode(Integer.parseInt(vals[i]));
if (isLeftChild) {
queue.get(index).left = node;
} else {
queue.get(index).right = node;
}
queue.add(node);
}
if (!isLeftChild) {
index++;
}
isLeftChild = !isLeftChild;
}
return root;
}
public String Serialize(TreeNode root) {
if (root == null) {
return null;
}
StringBuilder sb = new StringBuilder();
ArrayList<TreeNode> queue = new ArrayList<>();
queue.add(root);
for (int i = 0; i < queue.size(); i++) {
TreeNode node = queue.get(i);
if (node == null)
continue;
queue.add(node.left);
queue.add(node.right);
}
for (int i = 0; i < queue.size(); i++) {
if (queue.get(i) == null)
sb.append("#,");
else
sb.append("," + queue.get(i));
}
return sb.toString();
}
public TreeNode getParentRecursively(TreeNode root, TreeNode node) {
// 非递归实现查找某节点的父节点
root = this.root;//从树的根结点开始找
TreeNode temp;
if (root == null) {
return null;
}
if (node == root.left || node == root.right) {
return root;
}
if ((temp = getParentRecursively(root.left, node)) != null) {
return temp;
}
if ((temp = getParentRecursively(root.right, node)) != null) {
return temp;
}
return null;
}
public TreeNode getParentLooply(TreeNode node) {
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
TreeNode currentRoot = this.root;
stack.push(null);
while (currentRoot != null) {
if (node == currentRoot.left || node == currentRoot.right) {
return currentRoot;
}
if (currentRoot.right != null) {
stack.push(currentRoot.right);
}
if (currentRoot.left != null) {
currentRoot = currentRoot.left;
} else {
currentRoot = stack.pop();
}
}
return null;
}
public void traversalRecursively(TreeNode root) {
if (root != null) {
//visit(root);this is preorder
traversalRecursively(root.left);
// visit(root);this is inorder
traversalRecursively(root.right);
// visit(root);this is postoder
}
}
public ArrayList<Integer> preOrder() {
ArrayList<Integer> preOrder = new ArrayList<>();
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
if (root == null)
return preOrder;
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
preOrder.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return preOrder;
}
public ArrayList<Integer> inOrder() {
ArrayList<Integer> inorder = new ArrayList<>();
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
if (root == null) {
return inorder;
}
TreeNode ptr = root;
while (ptr != null || !stack.isEmpty()) {
while (ptr != null) {
stack.push(ptr);
ptr = ptr.left;
}
ptr = stack.pop();
inorder.add(ptr.val);
ptr = ptr.right;
}
return inorder;
}
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
// write your code here
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
if (root == null)
return res;
queue.offer(root);
while (!queue.isEmpty()) {
ArrayList<Integer> level = new ArrayList<>();
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
}
res.add(level);
}
return res;
}
}