ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2022-2-25 剑指offer day15

2022-02-25 10:03:56  阅读:170  来源: 互联网

标签:25 TreeNode val int day15 2022 null root 节点


题1:

JZ84 二叉树中和为某一值的路径(三)

描述

给定一个二叉树root和一个整数值 sum ,求该树有多少路径的的节点值之和等于 sum 。 1.该题路径定义不需要从根节点开始,也不需要在叶子节点结束,但是一定是从父亲节点往下到孩子节点 2.总节点数目为n 3.保证最后返回的路径个数在整形范围内(即路径个数小于231-1)   数据范围: 0<=n<=10000<=n<=1000
-10^9<=节点值<=10^9−109<=节点值<=109
  假如二叉树root为{1,2,3,4,5,4,3,#,#,-1},sum=6,那么总共如下所示,有3条路径符合要求
 1 import java.util.*;
 2 
 3 /*
 4  * public class TreeNode {
 5  *   int val = 0;
 6  *   TreeNode left = null;
 7  *   TreeNode right = null;
 8  *   public TreeNode(int val) {
 9  *     this.val = val;
10  *   }
11  * }
12  */
13 
14 public class Solution {
15     /**
16      * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
17      *
18      * 
19      * @param root TreeNode类 
20      * @param sum int整型 
21      * @return int整型
22      */
23     int s=0;
24     int ans;
25     
26     public int FindPath (TreeNode root, int sum) {
27         // write code here
28         int res=0;
29         if (root==null) return res;
30         Queue<TreeNode> queue=new LinkedList<>();
31         queue.offer(root);
32         while (!queue.isEmpty()){
33             TreeNode temp=queue.poll();
34             ans=0;
35             res+=FromThisToEnd(temp,sum);
36             if (temp.left!=null) queue.offer(temp.left);
37             if (temp.right!=null) queue.offer(temp.right);
38             //System.out.println(temp.val);
39         }
40         return res;
41     }
42     
43     
44     public int FromThisToEnd(TreeNode root,int sum){
45         if (root!=null) {
46            s+=root.val;
47            if (s==sum) ans++;
48            FromThisToEnd(root.left,sum);
49            FromThisToEnd(root.right,sum);
50            s-=root.val;
51         }
52         return ans;
53     }
54 }

思路:自定义函数计算当前节点满足条件的数量,遍历树的所有节点,累加结果。

 

题2:

JZ86 在二叉树中找到两个节点的最近公共祖先

 

描述

给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点。   数据范围:1 \le n \le 10001≤n≤1000,树上每个节点的val满足 0<val \le 1000<val≤100 要求:时间复杂度 O(n)O(n)   注:本题保证二叉树中每个节点的val值均不相同。   如当输入[3,5,1,6,2,0,8,#,#,7,4],5,1时,二叉树{3,5,1,6,2,0,8,#,#,7,4}如下图所示: 所以节点值为5和节点值为1的节点的最近公共祖先节点的节点值为3,所以对应的输出为3。 节点本身可以视为自己的祖先
 1 import java.util.*;
 2 
 3 /*
 4  * public class TreeNode {
 5  *   int val = 0;
 6  *   TreeNode left = null;
 7  *   TreeNode right = null;
 8  * }
 9  */
10 
11 public class Solution {
12     /**
13      * 
14      * @param root TreeNode类 
15      * @param o1 int整型 
16      * @param o2 int整型 
17      * @return int整型
18      */
19     Map<Integer,Integer> map;
20     public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
21         // write code here
22         map=new HashMap<>();
23         dfs(root);
24         Set<Integer> set1=new HashSet<>();
25         set1.add(o1);
26         while (map.containsKey(o1)) {
27             o1=map.get(o1);
28             set1.add(o1);
29         }
30         //for (int s:set1) System.out.print(s+" ");
31         while (!set1.contains(o2)){
32             o2=map.get(o2);
33         }
34         return o2;
35     }
36     
37     public void dfs(TreeNode root){
38         if (root==null) return;
39         if (root.left!=null) {
40             map.put(root.left.val,root.val);
41             dfs(root.left);
42             //System.out.println(root.left.val+"->"+root.val);
43         }
44         if (root.right!=null) {
45             map.put(root.right.val,root.val);
46             dfs(root.right);
47             //System.out.println(root.right.val+"->"+root.val);
48         }
49         
50        
51     }
52 }

思路:遍历树,因为节点值不同,利用hashmap将节点的祖先存入map,遍历一个节点的所有祖先存入set,再遍历另外一个节点的祖先出现重复就是结果。注意,自己也是自己的祖先,所以要把自己也放入集合。

标签:25,TreeNode,val,int,day15,2022,null,root,节点
来源: https://www.cnblogs.com/benbicao/p/15934617.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有