-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryTree.java
More file actions
248 lines (239 loc) · 8.51 KB
/
Copy pathBinaryTree.java
File metadata and controls
248 lines (239 loc) · 8.51 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package datastructure.tree;
import common.Node;
import common.TreeNode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class BinaryTree {
//递归算法-前序遍历(中序、后序稍作改动)
private List<Integer> list=new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root!=null){
list.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
}
return list;
}
//递归算法-树的最大深度
public int maxDepth(TreeNode root){
if(root==null){
return 0;
}
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return 1+Math.max(left,right);
}
//递归算法-检查二叉树否是对称。
public boolean isSymmetric(TreeNode root) {
return isMirror(root,root);
}
public boolean isMirror(TreeNode node1,TreeNode node2){
if(node1==null&&node2==null){
return true;
}
if(node1==null||node2==null){
return false;
}
return (node1.val==node2.val)&&isMirror(node1.left,node2.right)&&isMirror(node1.right,node2.left);
}
//给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null){
return false;
}
if(root.left == null && root.right == null){
return sum - root.val == 0;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
//从中序与后序遍历序列构造二叉树
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(postorder.length==0||inorder.length==0){
return null;
}
if(postorder.length!=inorder.length){
return null;
}
int rootVal=postorder[postorder.length-1];
TreeNode root=new TreeNode(rootVal);
int rootIndex=-1;
for(int i=0;i<inorder.length;i++){
if(inorder[i]==rootVal){
rootIndex=i;
break;
}
}
//也可以不用这么多temp数组,直接操作原数组,给定startIndex和endIndex,这样的话,参数是6个,但执行效率更快。
int[] inorderLeft=new int[rootIndex];
int[] inorderRight=new int[inorder.length-rootIndex-1];
int[] postorderLetf=new int[rootIndex];
int[] postorderRight=new int[inorder.length-rootIndex-1];
for(int i=0;i<rootIndex;i++){
inorderLeft[i]=inorder[i];
postorderLetf[i]=postorder[i];
}
int j=0;
for(int k=rootIndex+1;k<inorder.length;k++,j++){
inorderRight[j]=inorder[k];
postorderRight[j]=postorder[k-1];
}
root.left=buildTree(inorderLeft,postorderLetf);
root.right=buildTree(inorderRight,postorderRight);
return root;
}
//从前序与中序遍历序列构造二叉树
public TreeNode buildTree2(int[] preorder, int[] inorder) {
if(preorder == null || inorder == null || preorder.length==0){
return null;
}
return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
public TreeNode build(int[] preorder, int preLeft,int preRight,int[] inorder,int inLeft,int inRight){
if (inLeft > inRight || preLeft >preRight) {
return null;
}
int rootValue=preorder[preLeft];
TreeNode root=new TreeNode(rootValue);
if(preLeft==preRight){
return root;
}
int rootIndex=-1;
for(int i=inLeft;i<=inRight;i++){
if(inorder[i]==rootValue){
rootIndex=i;
}
}
//左子树的长度
int leftLength=rootIndex-inLeft;
//前序序列中左子树的最后一个节点
int leftPreEnd = preLeft + leftLength;
root.left=build(preorder,preLeft+1,leftPreEnd,inorder,inLeft,rootIndex-1);
root.right=build(preorder,leftPreEnd+1,preRight,inorder,rootIndex+1,inRight);
return root;
}
//填充每个节点的下一个右侧节点指针
//给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点
public Node connect(Node root) {
if(root==null){
return null;
}
if(root.left!=null){
//完美二叉树,所以有左孩子必定有右孩子
root.left.next=root.right;
if(root.next!=null){
root.right.next=root.next.left;
}
}
connect(root.left);
connect(root.right);
return root;
}
//填充每个节点的下一个右侧节点指针 II
//填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
public Node connect2(Node root) {
if(root==null){
return null;
}
Node rootNextFirstChildNode=null;
Node p=root.next;
//找到root的兄弟节点中从左往右第一个不为空的孩子节点。
while(p!=null){
if(p.left!=null){
rootNextFirstChildNode=p.left;
break;
}else if(p.right!=null){
rootNextFirstChildNode=p.right;
break;
}
p=p.next;
}
if(root.left!=null){
root.left.next=root.right==null?rootNextFirstChildNode:root.right;
}
if(root.right!=null){
root.right.next=rootNextFirstChildNode;
}
connect2(root.right);
connect2(root.left);
return root;
}
public static String serialize(TreeNode root) {
List<Integer> result=new ArrayList<>();
LinkedList<TreeNode> queue=new LinkedList<>();
List<Integer> temp=new ArrayList<>();
if(root==null){
return result.toString();
}
queue.offer(root);
boolean flag=true;
while(!queue.isEmpty()&&flag){
flag=false;
result.addAll(temp);
temp.clear();
int n=queue.size();
for(int i=0;i<n;i++){
TreeNode node=queue.poll();
if(node==null){
temp.add(null);
queue.offer(null);
queue.offer(null);
}else{
temp.add(node.val);
queue.offer(node.left);
queue.offer(node.right);
flag=true;
}
}
}
return result.toString().replace(" ","" );
}
public static TreeNode deserialize(String data) {
if(data.length()<=2){
return null;
}
String data2=data.substring(1,data.length()-1);
List<String> list=Arrays.asList(data2.split(","));
TreeNode root=new TreeNode(Integer.parseInt(list.get(0)));
LinkedList<TreeNode> queue=new LinkedList<>();
queue.add(root);
for(int i=1;Math.pow(2,i)<=list.size();i++) {
for (int j = (int) (Math.pow(2, i) - 1); j < Math.pow(2, i + 1) - 1; j=j+2) {
String left = list.get(j);
TreeNode leftNode = String2Node(left);
TreeNode rightNode=null;
if(j+1<Math.pow(2, i + 1) - 1){
String right = list.get(j + 1);
rightNode= String2Node(right);
}
TreeNode oldNode = queue.poll();
oldNode.left = leftNode;
oldNode.right = rightNode;
if (leftNode != null) {
queue.offer(leftNode);
}
if (rightNode != null) {
queue.offer(rightNode);
}
}
}
return root;
}
public static TreeNode String2Node(String s){
TreeNode node;
if ("null".equals(s)) {
node = null;
} else {
int val = Integer.parseInt(s);
node = new TreeNode(val);
}
return node;
}
public static void main(String[] args){
TreeNode result=deserialize("[1,2,3,null,null,4,5]");
System.out.println(serialize(result));
TreeNode result2=deserialize(serialize(result));
System.out.println(serialize(result2));
}
}