Skip to content

Commit 02106a7

Browse files
committed
20181028 add Tree
1 parent 555e921 commit 02106a7

11 files changed

+297
-169
lines changed

src/main/java/leetcode/tree/PopulatingNextRightPointersInEachNode.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/main/java/leetcode/tree/SumRootToLeafNumbers.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/leetcode/tree/ConvertSortedArrayToBST.java renamed to src/main/java/leetcode/tree/_108_ConvertSortedArrayToBST.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
* -10 5
2020
* 输出结果无标准答案
2121
*/
22-
public class ConvertSortedArrayToBST {
22+
public class _108_ConvertSortedArrayToBST {
23+
2324
public TreeNode sortedArrayToBST(int[] num){
2425
if(num==null||num.length==0){
2526
return null;
@@ -44,7 +45,7 @@ public TreeNode BST(int[] num,int left,int right){
4445
}
4546
public static void main(String[] args){
4647
int[] nums={2,4,6,8,9,10};
47-
ConvertSortedArrayToBST test=new ConvertSortedArrayToBST();
48+
_108_ConvertSortedArrayToBST test=new _108_ConvertSortedArrayToBST();
4849
TreeNode root=test.sortedArrayToBST(nums);
4950

5051
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package leetcode.tree;
2+
/**
3+
* 给定一个二叉树
4+
* <p>
5+
* struct TreeLinkNode {
6+
* TreeLinkNode *left;
7+
* TreeLinkNode *right;
8+
* TreeLinkNode *next;
9+
* }
10+
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
11+
* <p>
12+
* 初始状态下,所有 next 指针都被设置为 NULL。
13+
* <p>
14+
* 说明:
15+
* <p>
16+
* 你只能使用额外常数空间。
17+
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
18+
* 你可以假设它是一个完美二叉树(即所有叶子节点都在同一层,每个父节点都有两个子节点)。
19+
* 示例:
20+
* <p>
21+
* 给定完美二叉树,
22+
* <p>
23+
* 1
24+
* / \
25+
* 2 3
26+
* / \ / \
27+
* 4 5 6 7
28+
* 调用你的函数后,该完美二叉树变为:
29+
* <p>
30+
* 1 -> NULL
31+
* / \
32+
* 2 -> 3 -> NULL
33+
* / \ / \
34+
* 4->5->6->7 -> NULL
35+
* https://www.cnblogs.com/ariel-dreamland/p/9165670.html
36+
*/
37+
38+
import java.util.LinkedList;
39+
40+
class TreeLinkNode {
41+
int val;
42+
TreeLinkNode left, right, next;
43+
44+
TreeLinkNode(int x) {
45+
val = x;
46+
}
47+
}
48+
49+
public class _116_PopulatingNextRightPointersInEachNode {
50+
//LTM
51+
public void connect1(TreeLinkNode root) {
52+
if (root == null) {
53+
return;
54+
}
55+
LinkedList<TreeLinkNode> queue = new LinkedList<>();
56+
queue.offer(root);
57+
int current = 1;
58+
int next = 0;
59+
while (!queue.isEmpty()) {
60+
TreeLinkNode temp = queue.poll();
61+
current--;
62+
if (current != 0) {
63+
temp.next = queue.peek();
64+
} else {
65+
temp.next = null;
66+
}
67+
if (root.left != null) {
68+
queue.offer(root.left);
69+
next++;
70+
}
71+
if (root.right != null) {
72+
queue.offer(root.right);
73+
next++;
74+
}
75+
if (current == 0) {
76+
current = next;
77+
next = 0;
78+
}
79+
80+
}
81+
82+
}
83+
84+
public void connect2(TreeLinkNode root) {
85+
if (root == null) {
86+
return;
87+
}
88+
LinkedList<TreeLinkNode> queue = new LinkedList<>();
89+
queue.offer(root);
90+
while (!queue.isEmpty()) {
91+
int len = queue.size();
92+
for (int i = 0; i < len; i++) {
93+
TreeLinkNode temp = queue.poll();
94+
if (i == len - 1) {
95+
temp.next = null;
96+
} else {
97+
temp.next = queue.peek();
98+
}
99+
}
100+
if (root.left != null) {
101+
queue.offer(root.left);
102+
}
103+
if (root.right != null) {
104+
queue.offer(root.right);
105+
}
106+
}
107+
}
108+
109+
110+
public void connect(TreeLinkNode root) {
111+
TreeLinkNode levelFirst = root;
112+
TreeLinkNode cur = levelFirst;
113+
while (cur != null) {
114+
//给左指针添加next指针
115+
if (cur.left != null) {
116+
cur.left.next = cur.right;
117+
}
118+
//给右指针添加next指针
119+
if (cur.right != null && cur.next != null) {
120+
cur.right.next = cur.next.left;
121+
}
122+
//移动指针的位置
123+
if (cur.next != null) {
124+
cur = cur.next;
125+
} else {
126+
levelFirst = levelFirst.left;
127+
cur = levelFirst;
128+
}
129+
}
130+
}
131+
132+
}

src/main/java/leetcode/tree/PopulatingNextRightPointersInEachNode2.java renamed to src/main/java/leetcode/tree/_117_PopulatingNextRightPointersInEachNode2.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,42 @@
22

33
import java.util.LinkedList;
44

5-
public class PopulatingNextRightPointersInEachNode2 {
6-
//内存超限????
5+
/**
6+
* 给定一个二叉树
7+
* <p>
8+
* struct TreeLinkNode {
9+
* TreeLinkNode *left;
10+
* TreeLinkNode *right;
11+
* TreeLinkNode *next;
12+
* }
13+
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,
14+
* 则将 next 指针设置为 NULL。
15+
* <p>
16+
* 初始状态下,所有 next 指针都被设置为 NULL。
17+
* <p>
18+
* 说明:
19+
* <p>
20+
* 你只能使用额外常数空间。
21+
* 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
22+
* 示例:
23+
* <p>
24+
* 给定二叉树,
25+
* <p>
26+
* 1
27+
* / \
28+
* 2 3
29+
* / \ \
30+
* 4 5 7
31+
* 调用你的函数后,该二叉树变为:
32+
* <p>
33+
* 1 -> NULL
34+
* / \
35+
* 2 -> 3 -> NULL
36+
* / \ \
37+
* 4-> 5 -> 7 -> NULL
38+
*/
39+
public class _117_PopulatingNextRightPointersInEachNode2 {
40+
//内存超限
741
public void connect1(TreeLinkNode root) {
842
TreeLinkNode level = root, cur = level;
943
while (cur != null) {
@@ -60,40 +94,8 @@ public void connect2(TreeLinkNode root) {
6094
}
6195

6296
}
63-
//内存超限
64-
public void connect3(TreeLinkNode root) {
65-
if (root == null) {
66-
return;
67-
}
68-
LinkedList<TreeLinkNode> queue = new LinkedList<>();
69-
queue.offer(root);
70-
TreeLinkNode curlevel=root,nextlevel=root,last=null;
71-
while (!queue.isEmpty()) {
72-
TreeLinkNode temp = queue.poll();
73-
if(last!=null){
74-
last.next=temp;
75-
}
76-
if(temp==curlevel){
77-
temp.next=null;
78-
last=null;
79-
}else{
80-
last=temp;
81-
}
82-
if(temp.left!=null){
83-
nextlevel=temp.left;
84-
queue.add((temp.left));
85-
}
86-
if(temp.right!=null){
87-
nextlevel=temp.right;
88-
queue.add(temp.right);
89-
}
90-
if(temp==curlevel){
91-
curlevel=nextlevel;
92-
}
9397

94-
}
95-
96-
}
97-
9898

9999
}
100+
101+

src/main/java/leetcode/tree/BinaryTreeMaximumPathSum.java renamed to src/main/java/leetcode/tree/_124_BinaryTreeMaximumPathSum.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Return11
1717
注意:树的节点的值root.val可以是负数
1818
*/
19-
public class BinaryTreeMaximumPathSum {
19+
public class _124_BinaryTreeMaximumPathSum {
2020

2121
int maxsum=Integer.MIN_VALUE;
2222
public int maxPathSum(TreeNode root) {
@@ -62,7 +62,7 @@ public static void main(String[] args) {
6262
root2 = common.createTrees(arr2);
6363
root3 = common.createTrees(arr3);
6464
root4 = common.createTrees(arr4);
65-
BinaryTreeMaximumPathSum test=new BinaryTreeMaximumPathSum();
65+
_124_BinaryTreeMaximumPathSum test=new _124_BinaryTreeMaximumPathSum();
6666
//System.out.println(test.maxPathSum(root1));
6767
//System.out.println(test.maxPathSum(root2));
6868
System.out.println(test.maxPathSum(root3));

0 commit comments

Comments
 (0)