赞
踩
思路一:采用二叉树的递归的做法。
我们要先分清楚有几种情况:
情况一:找到感染节点后,感染节点的左右子树有可能决定所需时间
res = Math.max(num1,num2);
情况二:若某一个节点的左右子树都没有找到感染节点,则左右子树的高度有可能决定感染时间。
res = Math.max(Math.max(num1, num2)+1, res);
情况三:若某一个节点的左右子树中,有一个子树找到了感染节点,则 感染节点到该节点的路径+另一子树的最大高度 有可能决定感染时间。
res = Math.max(num1+num2+1, res);
如果找到感染节点,那么我们需要返回感染节点到该节点的路径。如果没找到,则返回该节点左右子树的最大高度。
因此在递归的过程中,我们需要知道两个返回值,一个是返回的高度,一个是是否找到感染的节点。(可是函数只能有有个返回值,因此这里用返回值来标记高度,用flag数组(开辟一个空间)来标记是否找到感染节点)
代码:
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
- int res = 0;
- public int amountOfTime(TreeNode root, int start) {
- int[] flag = new int[1];
- flag[0] = 0; //用来标记在该分支是否找到start节点。
- find(root,start,flag);
- return res;
- }
-
- int find(TreeNode head, int start, int[] flag) {
-
- if(head == null) {
- return 0;
- }
- if(head.val == start) {
- // 查找左右结点的高度。返回最大的节点数。
- int num1 = find(head.left,start,flag);
- int num2 = find(head.right,start,flag);
- flag[0] = 1;//当前已经找到,所以将flag设置为1、
- res = Math.max(num1,num2);
- return 0;
- }
- int re;//记录要返回的数值。如果找到start返回的分支,返回start到该节点的路径长度。
- //否则的话,返回左右子树中的最大高度。
- flag[0] = 0;
- int num1 = find(head.left,start,flag);//先找左分支、
- int num2;
- if(flag[0] == 1) {
- re = num1+1;
- num2 = find(head.right,start,flag);
- res = Math.max(num1+num2+1, res);//更新res值。
- flag[0] = 1;
- } else {
-
- num2 = find(head.right,start,flag);
- if(flag[0] == 1) {
- re = num2+1;
- res = Math.max(num1+num2+1, res);
-
- } else {
- re = Math.max(num1, num2)+1;
- res = Math.max(Math.max(num1, num2)+1, res);
- }
-
- }
- return re;
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
法二:用hashmap来记录每个结点的相邻结点,即父节点和子节点。
然后使用dfs来遍历所需要的最大时间
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode() {}
- * TreeNode(int val) { this.val = val; }
- * TreeNode(int val, TreeNode left, TreeNode right) {
- * this.val = val;
- * this.left = left;
- * this.right = right;
- * }
- * }
- */
- class Solution {
-
- Map<Integer, Set<Integer>> map = new HashMap<>();
- Set<Integer> set = new HashSet<>();
- int res = 0;
- public int amountOfTime(TreeNode root, int start) {
- search(root);
- //System.out.println(map);
- dfs(start,0);
- return res;
- }
- public void search(TreeNode root){
- if(root != null){
- Set<Integer> set = map.getOrDefault(root.val, new HashSet<>());
- if(root.left!=null){
- int child = root.left.val;
- Set<Integer> child_set = map.getOrDefault(root.left.val, new HashSet<>());
- set.add(child);
- child_set.add(root.val);
- map.put(root.left.val,child_set);
- }
- if(root.right!=null){
- int child = root.right.val;
- Set<Integer> child_set = map.getOrDefault(root.right.val, new HashSet<>());
- set.add(child);
- child_set.add(root.val);
- map.put(root.right.val,child_set);
- }
- map.put(root.val,set);
- search(root.left);
- search(root.right);
- }
- }
- public void dfs (int x, int step){
-
- if(set.contains(x)){
- return;
- }else{
- res = Math.max(step,res);
- //System.out.println("x=" + x + " step=" + step +" "+ " res=" + res );
- set.add(x);
- Set<Integer> list = map.get(x);
- if(list==null){
- return;
- }
- for(Integer y: list){
- dfs(y,step+1);
- }
- }
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。