代码随想录算法训练营第21天 | 236. 二叉树的最近公共祖先,501. 二叉搜索树中的众数,530. 二叉搜索树的最小绝对差

代码随想录算法训练营第21天 | 236. 二叉树的最近公共祖先,501. 二叉搜索树中的众数,530. 二叉搜索树的最小绝对差

530. 二叉搜索树的最小绝对差

  1. 二叉搜索树是有序的 中序遍历把节点值从小到大可以放到数组中 然后遍历 求最小绝对差值
/**
 * 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 {
    List<Integer> arr = new ArrayList<>();
    public int getMinimumDifference(TreeNode root) {
        int res = Integer.MAX_VALUE;
        //把节点值按从小到大的顺序装入数组
        traversal(root);
        for(int i =1;i<arr.size();i++){
            res = Math.min(res,arr.get(i)-arr.get(i-1));
        }
        return res;

    }

    public void traversal(TreeNode node){
        //终止条件
        if(node==null) return;

        //中序遍历
        traversal(node.left);
        arr.add(node.val);
        traversal(node.right);
        return;
    }
}

用双指针来遍历 求差值

/**
 * 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 = Integer.MAX_VALUE;
    TreeNode pre = null;
    public int getMinimumDifference(TreeNode root) {
        traversal(root);
        return res;

    }

    //双指针
    public void traversal(TreeNode cur){
        //终止条件
        if(cur==null) return;

        //中序遍历
        traversal(cur.left);
        if(pre!=null){
            res = Math.min(res,cur.val-pre.val);

        }
        pre = cur; //pre 跟在 cur后面 
        traversal(cur.right);
        return;

    } 
        
}

501. 二叉搜索树中的众数

  1. 将重复值出现次数存入map 求出最多次数的 对应值
/**
 * 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;
 *     }
 * }
 */
import java.util.Collection;
class Solution {
    Map<Integer,Integer> map = new HashMap<>();
    public int[] findMode(TreeNode root) {
        traversal(root);
        Collection<Integer> valuelist = map.values();
        int max = 0;
        for(Integer num:valuelist){
            max = Math.max(max,num);
        }
        return getKey(max);

    }

    public void traversal(TreeNode node){
        //终止条件
        if(node==null) return;

        //中序遍历
        traversal(node.left);
        map.put(node.val,map.getOrDefault(node.val, 0) + 1);
        traversal(node.right);
        return;
    }

    public int[] getKey(int value){
        List<Integer> res = new ArrayList<>();
        for(Integer key: map.keySet()){
            if(map.get(key)==value){
                res.add(key);
            }
        }
        
        int[] result = new int[res.size()];
        for(int i =0;i<res.size();i++){
            result[i]=res.get(i);
        }
        return result;
    }
}

双指针 只遍历一次

  1. 如何边遍历边将最大次数存入结果 可以设置一个maxcount 当count==maxcount时 存入结果集
  2. 当count>maxcount时 清空结果集 更新maxcount 存入结果集
/**
 * 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 count = 0;
    int maxcount = 0;
    TreeNode pre = null;
    List<Integer> result = new ArrayList<>();

    public int[] findMode(TreeNode root) {
        traversal(root);
        int[] res = new int[result.size()];
        for(int i =0;i<result.size();i++){
            res[i]=result.get(i);
        }
        return res;

    }
    //双指针
    public void traversal(TreeNode cur){
        //终止条件
        if(cur==null) return;

        //中序遍历
        //左
        traversal(cur.left);
        //中
        if(pre==null){
            count=1;
        }else if(cur.val==pre.val){
            count++;
        }else{
            count=1;
        }
        pre=cur;

        //判断是否等于maxcount
        if(count==maxcount){
            result.add(cur.val);
        }
        if(count>maxcount){
            result.clear();
            maxcount=count;
            result.add(cur.val);
        }
        //右
        traversal(cur.right);

        return;
    }
        
}

236. 二叉树的最近公共祖先

  1. 需要从底部向上查找 用后序遍历 判断左右是否有目标节点 有则往上面返回
  2. 若遇到 某个父节点 的左右子树的返回值 都不为Null 那么就返回这个父节点root 它就是最小祖先
  3. 若一直找到底都没有遇到pq 就返回Null回去
  4. 还有一种可能是 p为q的父节点 这种情况也可以处理 因为只要遇到P或q 就返回该节点
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //终止条件
        if(root==null) return null; //如果没有遇到pq 就返回null 回溯
        if(root==p || root==q) return root; //遇到pq就 返回 该节点 

        //后序遍历
        //看左子树 和 右子树 是否为空  若为空 则是没有遇到p ,q
        TreeNode left = lowestCommonAncestor(root.left,p,q);
        TreeNode right = lowestCommonAncestor(root.right,p,q);  
        //中
        if(left!=null && right!=null){
            return root;
        }

        if(left!=null && right==null) return left;
        if(left==null && right!=null) return right;
        if(left==null && right==null) return null;
        return root;





    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值