Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb42850

Browse files
committed
20190515
1 parent c86a4ba commit eb42850

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

‎code/lc559.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package code;
2+
3+
import java.util.List;
4+
5+
/*
6+
* 559. Maximum Depth of N-ary Tree
7+
* 题意:多叉树的最大深度
8+
* 难度:Easy
9+
* 分类:Tree, Depth-first Search, Breadth-first Search
10+
* 思路:BFS 也能做和 lc104思路一样
11+
* Tips:lc104, lc111
12+
*/
13+
14+
public class lc559 {
15+
class Node {
16+
public int val;
17+
public List<Node> children;
18+
19+
public Node() {}
20+
21+
public Node(int _val,List<Node> _children) {
22+
val = _val;
23+
children = _children;
24+
}
25+
}
26+
27+
public int maxDepth(Node root) {
28+
if(root==null) return 0;
29+
int res = 0;
30+
for (Node nd : root.children) {
31+
res = Math.max(maxDepth(nd), res);
32+
}
33+
return res+1;
34+
}
35+
36+
}

‎code/lc589.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
* 589. N-ary Tree Preorder Traversal
8+
* 题意:多叉树先序遍历
9+
* 难度:Easy
10+
* 分类:Tree
11+
* 思路:
12+
* Tips:
13+
*/
14+
public class lc589 {
15+
class Node {
16+
public int val;
17+
public List<Node> children;
18+
19+
public Node() {}
20+
21+
public Node(int _val,List<Node> _children) {
22+
val = _val;
23+
children = _children;
24+
}
25+
}
26+
27+
List<Integer> res;
28+
public List<Integer> preorder(Node root) {
29+
res = new ArrayList<>();
30+
helper(root);
31+
return res;
32+
}
33+
34+
public void helper(Node root){
35+
if(root==null) return;
36+
res.add(root.val);
37+
for(Node nd:root.children){
38+
helper(nd);
39+
}
40+
}
41+
}

‎code/lc590.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class lc590 {
7+
class Node {
8+
public int val;
9+
public List<Node> children;
10+
11+
public Node() {}
12+
13+
public Node(int _val,List<Node> _children) {
14+
val = _val;
15+
children = _children;
16+
}
17+
}
18+
19+
List<Integer> res;
20+
public List<Integer> postorder(Node root) {
21+
res = new ArrayList<>();
22+
helper(root);
23+
return res;
24+
}
25+
26+
public void helper(Node root){
27+
if(root==null) return;
28+
for(Node nd:root.children){
29+
helper(nd);
30+
}
31+
res.add(root.val);
32+
}
33+
}

‎code/lc834.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package code;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
/*
6+
* 834. Sum of Distances in Tree
7+
* 题意:求树中每个节点的值,到其他节点的距离和
8+
* 难度:Hard
9+
* 分类:Tree, Depth-first Search
10+
* 思路:真的难,我是做不出来
11+
* 两次遍历,第一次后续遍历,计算出每个节点为根的树,有几个孩子节点,并计算出root的结果
12+
* 根据前一步的计算结果,开始先序遍历,一步步计算出其他节点的结果
13+
* res[node] = res[parent]-2*count[node]+count.length;
14+
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130583/C%2B%2BJavaPython-Pre-order-and-Post-order-DFS-O(N)
15+
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130567/Two-traversals-O(N)-python-solution-with-Explanation
16+
* Tips:
17+
*/
18+
public class lc834 {
19+
public static void main(String[] args) {
20+
int[][] arr = {{0,1},{0,2},{2,3},{2,4},{2,5}};
21+
sumOfDistancesInTree(6, arr);
22+
}
23+
24+
static int[] count;
25+
static int[] res;
26+
static HashMap<Integer, HashSet<Integer>> hm = new HashMap();
27+
public static int[] sumOfDistancesInTree(int N, int[][] edges) {
28+
if(edges.length==0) return new int[]{0};
29+
count = new int[N]; //记录n为跟的树,下边有多少个节点
30+
res = new int[N];
31+
for (int i = 0; i < edges.length ; i++) { //双向都添加,遍历的时候判断一下,因为并不一定0就是根
32+
HashSet<Integer> hs = hm.getOrDefault(edges[i][0], new HashSet());
33+
hs.add(edges[i][1]);
34+
hm.put(edges[i][0], hs);
35+
hs = hm.getOrDefault(edges[i][1], new HashSet());
36+
hs.add(edges[i][0]);
37+
hm.put(edges[i][1], hs);
38+
}
39+
helper1(0, -1);
40+
helper2(0, -1);
41+
return res;
42+
}
43+
44+
public static int helper1(int node, int parent){ //后序遍历,求root对应的结果,计算每个节点的count
45+
HashSet<Integer> hs = hm.getOrDefault(node, new HashSet());
46+
for (Integer i:hs) {
47+
if(i==parent) continue; //是parent的话直接略过
48+
count[node] += helper1(i, node);
49+
res[node] += res[i] + count[i];
50+
}
51+
count[node]++;
52+
return count[node];
53+
}
54+
55+
public static void helper2(int node, int parent){ //先序遍历,求结果
56+
HashSet<Integer> hs = hm.getOrDefault(node, new HashSet());
57+
if(node!=0)
58+
res[node] = res[parent]-2*count[node]+count.length; //转移计算
59+
for (Integer i:hs) {
60+
if(i==parent) continue; //是parent的话直接略过
61+
helper2(i, node);
62+
}
63+
}
64+
}

‎readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,20 @@ LeetCode 指南
212212
| 494 [Java](./code/lc494.java)
213213
| 538 [Java](./code/lc538.java)
214214
| 543 [Java](./code/lc543.java)
215+
| 559 [Java](./code/lc559.java)
215216
| 560 [Java](./code/lc543.java)
216217
| 572 [Java](./code/lc572.java)
217218
| 581 [Java](./code/lc581.java)
219+
| 589 [Java](./code/lc589.java)
220+
| 590 [Java](./code/lc590.java)
218221
| 617 [Java](./code/lc617.java)
219222
| 621 [Java](./code/lc621.java)
220223
| 647 [Java](./code/lc647.java)
221224
| 685 [Java](./code/lc685.java)
222225
| 714 [Java](./code/lc714.java)
223226
| 746 [Java](./code/lc746.java)
224227
| 771 [Java](./code/lc771.java)
228+
| 834 [Java](./code/lc834.java)
225229
| 877 [Java](./code/lc877.java)
226230
| 921 [Java](./code/lc921.java)
227231
| 922 [Java](./code/lc922.java)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /