Skip to content

Commit b81a0ea

Browse files
增加二叉树的基本题目
1 parent 3ade29e commit b81a0ea

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/main/java/common/Node.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package common;
2+
3+
public class Node {
4+
public int val;
5+
public Node left;
6+
public Node right;
7+
public Node next;
8+
9+
public Node() {}
10+
11+
public Node(int _val,Node _left,Node _right,Node _next) {
12+
val = _val;
13+
left = _left;
14+
right = _right;
15+
next = _next;
16+
}
17+
}

src/main/java/datastructure/tree/BinaryTree.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datastructure.tree;
22

3+
import common.Node;
34
import common.TreeNode;
45

56
import java.util.ArrayList;
@@ -86,5 +87,81 @@ public TreeNode buildTree(int[] inorder, int[] postorder) {
8687
root.right=buildTree(inorderRight,postorderRight);
8788
return root;
8889
}
90+
//从前序与中序遍历序列构造二叉树
91+
public TreeNode buildTree2(int[] preorder, int[] inorder) {
92+
if(preorder == null || inorder == null || preorder.length==0){
93+
return null;
94+
}
95+
return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
96+
}
97+
public TreeNode build(int[] preorder, int preLeft,int preRight,int[] inorder,int inLeft,int inRight){
98+
if (inLeft > inRight || preLeft >preRight) {
99+
return null;
100+
}
101+
int rootValue=preorder[preLeft];
102+
TreeNode root=new TreeNode(rootValue);
103+
if(preLeft==preRight){
104+
return root;
105+
}
106+
int rootIndex=-1;
107+
for(int i=inLeft;i<=inRight;i++){
108+
if(inorder[i]==rootValue){
109+
rootIndex=i;
110+
}
111+
}
112+
//左子树的长度
113+
int leftLength=rootIndex-inLeft;
114+
//前序序列中左子树的最后一个节点
115+
int leftPreEnd = preLeft + leftLength;
116+
root.left=build(preorder,preLeft+1,leftPreEnd,inorder,inLeft,rootIndex-1);
117+
root.right=build(preorder,leftPreEnd+1,preRight,inorder,rootIndex+1,inRight);
118+
return root;
119+
}
120+
//填充每个节点的下一个右侧节点指针
121+
//给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点
122+
public Node connect(Node root) {
123+
if(root==null){
124+
return null;
125+
}
126+
if(root.left!=null){
127+
//完美二叉树,所以有左孩子必定有右孩子
128+
root.left.next=root.right;
129+
if(root.next!=null){
130+
root.right.next=root.next.left;
131+
}
132+
}
133+
connect(root.left);
134+
connect(root.right);
135+
return root;
136+
}
137+
//填充每个节点的下一个右侧节点指针 II
138+
//填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
139+
public Node connect2(Node root) {
140+
if(root==null){
141+
return null;
142+
}
143+
Node rootNextFirstChildNode=null;
144+
Node p=root.next;
145+
//找到root的兄弟节点中从左往右第一个不为空的孩子节点。
146+
while(p!=null){
147+
if(p.left!=null){
148+
rootNextFirstChildNode=p.left;
149+
break;
150+
}else if(p.right!=null){
151+
rootNextFirstChildNode=p.right;
152+
break;
153+
}
154+
p=p.next;
155+
}
156+
if(root.left!=null){
157+
root.left.next=root.right==null?rootNextFirstChildNode:root.right;
158+
}
159+
if(root.right!=null){
160+
root.right.next=rootNextFirstChildNode;
161+
}
162+
connect2(root.right);
163+
connect2(root.left);
164+
return root;
165+
}
89166
}
90167

0 commit comments

Comments
 (0)