提交 eb428505 编写于 作者: L liu13

20190515

上级 c86a4ba9
package code;
import java.util.List;
/*
* 559. Maximum Depth of N-ary Tree
* 题意:多叉树的最大深度
* 难度:Easy
* 分类:Tree, Depth-first Search, Breadth-first Search
* 思路:BFS 也能做和 lc104思路一样
* Tips:lc104, lc111
*/
public class lc559 {
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
}
public int maxDepth(Node root) {
if(root==null) return 0;
int res = 0;
for (Node nd : root.children) {
res = Math.max(maxDepth(nd), res);
}
return res+1;
}
}
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 589. N-ary Tree Preorder Traversal
* 题意:多叉树先序遍历
* 难度:Easy
* 分类:Tree
* 思路:
* Tips:
*/
public class lc589 {
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
}
List<Integer> res;
public List<Integer> preorder(Node root) {
res = new ArrayList<>();
helper(root);
return res;
}
public void helper(Node root){
if(root==null) return;
res.add(root.val);
for(Node nd:root.children){
helper(nd);
}
}
}
package code;
import java.util.ArrayList;
import java.util.List;
public class lc590 {
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
}
List<Integer> res;
public List<Integer> postorder(Node root) {
res = new ArrayList<>();
helper(root);
return res;
}
public void helper(Node root){
if(root==null) return;
for(Node nd:root.children){
helper(nd);
}
res.add(root.val);
}
}
package code;
import java.util.HashMap;
import java.util.HashSet;
/*
* 834. Sum of Distances in Tree
* 题意:求树中每个节点的值,到其他节点的距离和
* 难度:Hard
* 分类:Tree, Depth-first Search
* 思路:真的难,我是做不出来
* 两次遍历,第一次后续遍历,计算出每个节点为根的树,有几个孩子节点,并计算出root的结果
* 根据前一步的计算结果,开始先序遍历,一步步计算出其他节点的结果
* res[node] = res[parent]-2*count[node]+count.length;
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130583/C%2B%2BJavaPython-Pre-order-and-Post-order-DFS-O(N)
* https://leetcode.com/problems/sum-of-distances-in-tree/discuss/130567/Two-traversals-O(N)-python-solution-with-Explanation
* Tips:
*/
public class lc834 {
public static void main(String[] args) {
int[][] arr = {{0,1},{0,2},{2,3},{2,4},{2,5}};
sumOfDistancesInTree(6, arr);
}
static int[] count;
static int[] res;
static HashMap<Integer, HashSet<Integer>> hm = new HashMap();
public static int[] sumOfDistancesInTree(int N, int[][] edges) {
if(edges.length==0) return new int[]{0};
count = new int[N]; //记录n为跟的树,下边有多少个节点
res = new int[N];
for (int i = 0; i < edges.length ; i++) { //双向都添加,遍历的时候判断一下,因为并不一定0就是根
HashSet<Integer> hs = hm.getOrDefault(edges[i][0], new HashSet());
hs.add(edges[i][1]);
hm.put(edges[i][0], hs);
hs = hm.getOrDefault(edges[i][1], new HashSet());
hs.add(edges[i][0]);
hm.put(edges[i][1], hs);
}
helper1(0, -1);
helper2(0, -1);
return res;
}
public static int helper1(int node, int parent){ //后序遍历,求root对应的结果,计算每个节点的count
HashSet<Integer> hs = hm.getOrDefault(node, new HashSet());
for (Integer i:hs) {
if(i==parent) continue; //是parent的话直接略过
count[node] += helper1(i, node);
res[node] += res[i] + count[i];
}
count[node]++;
return count[node];
}
public static void helper2(int node, int parent){ //先序遍历,求结果
HashSet<Integer> hs = hm.getOrDefault(node, new HashSet());
if(node!=0)
res[node] = res[parent]-2*count[node]+count.length; //转移计算
for (Integer i:hs) {
if(i==parent) continue; //是parent的话直接略过
helper2(i, node);
}
}
}
......@@ -212,9 +212,12 @@ LeetCode 指南
| 494 [Java](./code/lc494.java)
| 538 [Java](./code/lc538.java)
| 543 [Java](./code/lc543.java)
| 559 [Java](./code/lc559.java)
| 560 [Java](./code/lc543.java)
| 572 [Java](./code/lc572.java)
| 581 [Java](./code/lc581.java)
| 589 [Java](./code/lc589.java)
| 590 [Java](./code/lc590.java)
| 617 [Java](./code/lc617.java)
| 621 [Java](./code/lc621.java)
| 647 [Java](./code/lc647.java)
......@@ -222,6 +225,7 @@ LeetCode 指南
| 714 [Java](./code/lc714.java)
| 746 [Java](./code/lc746.java)
| 771 [Java](./code/lc771.java)
| 834 [Java](./code/lc834.java)
| 877 [Java](./code/lc877.java)
| 921 [Java](./code/lc921.java)
| 922 [Java](./code/lc922.java)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册