当前位置:   article > 正文

2022 0821 leetcode周赛_leetcode周赛预测

leetcode周赛预测

 思路一:采用二叉树的递归的做法。

我们要先分清楚有几种情况:

        情况一:找到感染节点后,感染节点的左右子树有可能决定所需时间  

                res = Math.max(num1,num2);

        情况二:若某一个节点的左右子树都没有找到感染节点,则左右子树的高度有可能决定感染时间。

                res = Math.max(Math.max(num1, num2)+1, res);

        情况三:若某一个节点的左右子树中,有一个子树找到了感染节点,则   感染节点到该节点的路径+另一子树的最大高度   有可能决定感染时间。

                res = Math.max(num1+num2+1, res);

        如果找到感染节点,那么我们需要返回感染节点到该节点的路径。如果没找到,则返回该节点左右子树的最大高度。

        因此在递归的过程中,我们需要知道两个返回值,一个是返回的高度,一个是是否找到感染的节点。(可是函数只能有有个返回值,因此这里用返回值来标记高度,用flag数组(开辟一个空间)来标记是否找到感染节点)

代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. int res = 0;
  18. public int amountOfTime(TreeNode root, int start) {
  19. int[] flag = new int[1];
  20. flag[0] = 0; //用来标记在该分支是否找到start节点。
  21. find(root,start,flag);
  22. return res;
  23. }
  24. int find(TreeNode head, int start, int[] flag) {
  25. if(head == null) {
  26. return 0;
  27. }
  28. if(head.val == start) {
  29. // 查找左右结点的高度。返回最大的节点数。
  30. int num1 = find(head.left,start,flag);
  31. int num2 = find(head.right,start,flag);
  32. flag[0] = 1;//当前已经找到,所以将flag设置为1、
  33. res = Math.max(num1,num2);
  34. return 0;
  35. }
  36. int re;//记录要返回的数值。如果找到start返回的分支,返回start到该节点的路径长度。
  37. //否则的话,返回左右子树中的最大高度。
  38. flag[0] = 0;
  39. int num1 = find(head.left,start,flag);//先找左分支、
  40. int num2;
  41. if(flag[0] == 1) {
  42. re = num1+1;
  43. num2 = find(head.right,start,flag);
  44. res = Math.max(num1+num2+1, res);//更新res值。
  45. flag[0] = 1;
  46. } else {
  47. num2 = find(head.right,start,flag);
  48. if(flag[0] == 1) {
  49. re = num2+1;
  50. res = Math.max(num1+num2+1, res);
  51. } else {
  52. re = Math.max(num1, num2)+1;
  53. res = Math.max(Math.max(num1, num2)+1, res);
  54. }
  55. }
  56. return re;
  57. }
  58. }

法二:用hashmap来记录每个结点的相邻结点,即父节点和子节点。

               然后使用dfs来遍历所需要的最大时间

                

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. Map<Integer, Set<Integer>> map = new HashMap<>();
  18. Set<Integer> set = new HashSet<>();
  19. int res = 0;
  20. public int amountOfTime(TreeNode root, int start) {
  21. search(root);
  22. //System.out.println(map);
  23. dfs(start,0);
  24. return res;
  25. }
  26. public void search(TreeNode root){
  27. if(root != null){
  28. Set<Integer> set = map.getOrDefault(root.val, new HashSet<>());
  29. if(root.left!=null){
  30. int child = root.left.val;
  31. Set<Integer> child_set = map.getOrDefault(root.left.val, new HashSet<>());
  32. set.add(child);
  33. child_set.add(root.val);
  34. map.put(root.left.val,child_set);
  35. }
  36. if(root.right!=null){
  37. int child = root.right.val;
  38. Set<Integer> child_set = map.getOrDefault(root.right.val, new HashSet<>());
  39. set.add(child);
  40. child_set.add(root.val);
  41. map.put(root.right.val,child_set);
  42. }
  43. map.put(root.val,set);
  44. search(root.left);
  45. search(root.right);
  46. }
  47. }
  48. public void dfs (int x, int step){
  49. if(set.contains(x)){
  50. return;
  51. }else{
  52. res = Math.max(step,res);
  53. //System.out.println("x=" + x + " step=" + step +" "+ " res=" + res );
  54. set.add(x);
  55. Set<Integer> list = map.get(x);
  56. if(list==null){
  57. return;
  58. }
  59. for(Integer y: list){
  60. dfs(y,step+1);
  61. }
  62. }
  63. }
  64. }
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号