Skip to content

Commit b101d0a

Browse files
committed
树的算法,新增层次遍历
1 parent 99dffd5 commit b101d0a

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package 数据结构..层次遍历;
2+
3+
import 数据结构..TreeNode;
4+
5+
import java.util.*;
6+
7+
/**
8+
* Created by 周杰伦 on 2018/4/24.
9+
*/
10+
public class 一棵树每层节点的平均数 {
11+
12+
public List<Double> averageOfLevels(TreeNode root) {
13+
if (root == null) return null;
14+
Queue<TreeNode> queue = new LinkedList<>();
15+
List<Double> list = new ArrayList<>();
16+
queue.add(root);
17+
//标识上一行的最后一个节点。
18+
TreeNode last = root;
19+
//标识最后遍历到的一个节点。
20+
TreeNode nlast = null;
21+
double temp = 0;
22+
double cnt= 0;
23+
while (!queue.isEmpty()) {
24+
TreeNode t = queue.poll();
25+
temp += t.val;
26+
cnt ++;
27+
if (t.left != null) {
28+
queue.add(t.left);
29+
nlast = t.left;
30+
}
31+
if (t.right != null) {
32+
queue.add(t.right);
33+
nlast = t.right;
34+
}
35+
36+
if (t == last) {
37+
last = nlast;
38+
list.add(temp/cnt);
39+
temp = 0;
40+
cnt = 0;
41+
}
42+
}
43+
return list;
44+
}
45+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package 数据结构..层次遍历;
2+
3+
import 数据结构..TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
10+
/**
11+
* Created by 周杰伦 on 2018/4/24.
12+
*/
13+
public class 得到左下角的节点 {
14+
//其实就是层次遍历的最后一个节点。
15+
public int findBottomLeftValue(TreeNode root) {
16+
Queue<TreeNode> queue = new LinkedList<>();
17+
queue.add(root);
18+
while (!queue.isEmpty()){
19+
root = queue.poll();
20+
if (root.right != null) queue.add(root.right);
21+
if (root.left != null) queue.add(root.left);
22+
}
23+
return root.val;
24+
}
25+
26+
//傻逼了,这样做复杂化了。
27+
// public int findBottomLeftValue(TreeNode root) {
28+
// if (root == null) return 0;
29+
//
30+
// Queue<TreeNode> queue = new LinkedList<>();
31+
// int depth = depth(root);
32+
// int line = 1;
33+
// queue.add(root);
34+
// //标识上一行的最后一个节点。
35+
// TreeNode last = root;
36+
// //标识最后遍历到的一个节点。
37+
// TreeNode nlast = null;
38+
// while (!queue.isEmpty()) {
39+
// TreeNode t = queue.poll();
40+
// if (line == depth) {
41+
// return t.val;
42+
// }
43+
// if (t.left != null) {
44+
// queue.add(t.left);
45+
// nlast = t.left;
46+
// }
47+
// if (t.right != null) {
48+
// queue.add(t.right);
49+
// nlast = t.right;
50+
// }
51+
//
52+
// if (t == last ) {
53+
// last = nlast;
54+
// line ++;
55+
// }
56+
// }
57+
// return root.val;
58+
// }
59+
//
60+
// public int depth (TreeNode root) {
61+
// if (root == null)return 0;
62+
// int left = depth(root.left);
63+
// int right = depth(root.right);
64+
// return (left > right ? left : right) + 1;
65+
// }
66+
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
11
package 数据结构..递归.路径;
22

3+
import 数据结构..TreeNode;
4+
35
/**
46
* Created by 周杰伦 on 2018/4/20.
57
*/
68
public class 相同节点值的最大路径长度 {
9+
private int path = 0;
10+
11+
public int longestUnivaluePath(TreeNode root) {
12+
dfs(root);
13+
return path;
14+
}
15+
16+
private int dfs(TreeNode root){
17+
if (root == null) return 0;
18+
int left = dfs(root.left);
19+
int right = dfs(root.right);
20+
int leftPath = root.left != null && root.left.val == root.val ? left + 1 : 0;
21+
int rightPath = root.right != null && root.right.val == root.val ? right + 1 : 0;
22+
path = Math.max(path, leftPath + rightPath);
23+
return Math.max(leftPath, rightPath);
24+
}
25+
//不知道哪里做错了,很烦
26+
// public int longestUnivaluePath(TreeNode root) {
27+
// if (root == null) return 0;
28+
// int max = 0;
29+
// int left = 0,right = 0;
30+
// if (root.left != null && root.val == root.left.val) {
31+
// left ++;
32+
// }
33+
// if (root.right != null && root.val == root.right.val) {
34+
// right ++;
35+
// }
36+
// left = recur(root.left, left);
37+
// right = recur(root.right, right);
38+
// max = left + right;
39+
// max = Math.max(longestUnivaluePath(root.left), max);
40+
// max = Math.max(longestUnivaluePath(root.right), max);
41+
// return max;
42+
// }
43+
// public int recur (TreeNode t,int cnt) {
44+
// if (t == null)return cnt;
45+
// int left = cnt;
46+
// int right = cnt;
47+
// if (t.left != null && t.val == t.left.val) {
48+
// left = recur(t.left, cnt + 1);
49+
// }
50+
// if (t.right != null && t.val == t.right.val) {
51+
// right = recur(t.right, cnt + 1);
52+
// }
53+
// return left > right ? left : right;
54+
// }
755
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,101 @@
11
package 数据结构..遍历;
22

3+
import 数据结构..TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
38
/**
49
* Created by 周杰伦 on 2018/4/20.
510
*/
611
public class 间隔遍历 {
12+
//前三行的抢法,要么抢13要么抢2,抢2的时候可以选择抢24或者抢3。。以此类推。
13+
//得到最大值。这个题要记
14+
public int rob(TreeNode root) {
15+
if (root == null) return 0;
16+
int val1 = root.val;
17+
if (root.left != null) {
18+
val1 += rob(root.left.left) + rob(root.left.right);
19+
}
20+
if (root.right != null) {
21+
val1 += rob(root.right.left) + rob(root.right.right);
22+
}
23+
int val2 = rob(root.left) + rob(root.right);
24+
return Math.max(val1, val2);
25+
}
26+
//思路有问题,告辞
27+
// public int rob(TreeNode root) {
28+
// if (root == null) return 0;
29+
// int depth = depth(root);
30+
// int max = 0;
31+
// if (depth == 2) {
32+
// max = Math.max(recur(root, 1), max);
33+
// max = Math.max(recur(root, 2), max);
34+
// return max;
35+
// }
36+
// int []visit = new int[depth + 1];
37+
// visit[0] = 1;
38+
// for (int i = 1;i <= depth;i ++) {
39+
// max = dfs(root, i, depth, 0, visit);
40+
// }
41+
//
42+
// }
43+
// public int dfs(TreeNode t,int cur,int depth, int cnt, int []visit) {
44+
// if (t == null)return 0;
45+
//
46+
// cnt += recur(t,cur);
47+
// visit[cur] = 1;
48+
//
49+
// for (int i = 1;i <= depth ;i ++) {
50+
// if (i - 1 == 0 && visit[i] != 1 && visit[i + 1] != 1) {
51+
// cnt = dfs(t, i, depth, cnt, visit);
52+
// }
53+
// if (i + 1 == depth + 1 && visit[i] != 1 && visit[i - 1] != 1) {
54+
// cnt = dfs(t, i, depth, cnt, visit);
55+
// }
56+
// if (visit[i] != 1 && i - 1 >= 1 && visit[i - 1] != 1 && visit[i + 1] != 1) {
57+
// cnt = dfs(t, i, depth, cnt, visit);
58+
// }
59+
// }
60+
// }
61+
// public int depth (TreeNode root) {
62+
// if (root == null)return 0;
63+
// int left = depth(root.left);
64+
// int right = depth(root.right);
65+
// return (left > right ? left : right) + 1;
66+
// }
67+
// public int recur(TreeNode root,int line) {
68+
// if (root == null) return 0;
69+
// Queue<TreeNode> queue = new LinkedList<>();
70+
// queue.add(root);
71+
// int max = 0;
72+
// //标识上一行的最后一个节点。
73+
// TreeNode last = root;
74+
// //标识最后遍历到的一个节点。
75+
// TreeNode nlast = null;
76+
// int cur = 1;
77+
// int temp = 0;
78+
// while (!queue.isEmpty()) {
79+
// TreeNode t = queue.poll();
80+
// temp += t.val;
81+
// if (t.left != null) {
82+
// queue.add(t.left);
83+
// nlast = t.left;
84+
// }
85+
// if (t.right != null) {
86+
// queue.add(t.right);
87+
// nlast = t.right;
88+
// }
89+
//
90+
// if (t == last) {
91+
// last = nlast;
92+
// if (line == cur) {
93+
// max = Math.max(max, temp);
94+
// }
95+
// temp = 0;
96+
// cur ++;
97+
// }
98+
// }
99+
// return max;
100+
// }
7101
}

0 commit comments

Comments
 (0)