Leetcode 刷题 二叉树

文章介绍了如何利用二叉搜索树的特性解决不同问题,包括找到树中节点间的最小绝对差、计算众数以及寻找最近公共祖先。在最小绝对差问题中,通过中序遍历得到有序序列;在求众数时,使用哈希映射统计节点值出现的次数;对于最近公共祖先,采用后序遍历自底向上寻找。
摘要由CSDN通过智能技术生成

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

 利用二叉搜索树的特性 

二叉搜索树采用中序遍历,其实就是一个有序数组

Integer.MAX_VALUE
Integer.MIN_VALUE

中序遍历

class Solution {
    TreeNode node = null;
    int Min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        inOrder(root);
        return Min;
    }

    private void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        // 左
        inOrder(root.left);
        
        //中
        if(node != null){
            int diff = Math.abs(root.val - node.val);
            if(diff < Min){
                Min = diff;
            }
        }
        node = root;

        //右
        inOrder(root.right);
    }
}

501. 二叉搜索树中的众数 

普通的二叉树 怎么求众数 

一些方法的回顾

Integer.MIN_VALUE   Integer.MAX_VALUE
map.values();  // 获取到所有的value
map.entrySet();   返回 Map.Entry<> 的集合
    entry.getKey();
    emtry.getValue();
map.getOrDefault();
class Solution {
    Map<Integer, Integer> map = null;
    public int[] findMode(TreeNode root) {
        map = new HashMap<>();
        inOrder(root);
        int max = Integer.MIN_VALUE;
        for(Integer val : map.values()){
            if(val > max){
                max = val;
            }
        }
        List<Integer> res = new ArrayList<>();
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            if(entry.getValue() == max){
                res.add(entry.getKey());
            }
        }

        int size = res.size();
        int[] ans = new int[size];
        for(int i = 0; i < size; i++){
            ans[i] = res.get(i);
        }
        return ans;

    }

    private void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        map.put(root.val, map.getOrDefault(root.val, 0) + 1);
        inOrder(root.right);
    }
}

 利用二叉搜索树的特性

用一个pre节点 在二叉树遍历的过程中不断记录前一个节点

来判断是否相等 并记录次数

class Solution {
    List<Integer> list;
    TreeNode pre;
    int count;
    int maxcount;
    public int[] findMode(TreeNode root) {
        list = new ArrayList<>();
        maxcount = 0;
        pre = null;
        count = 0;
        inOrder(root);
        int[] res = new int[list.size()];

        for(int i = 0; i < list.size(); i++){
            res[i] = list.get(i);
        }
        return res;
    }

    private void inOrder(TreeNode root){
        if(root == null){
            return;
        }
        inOrder(root.left);
        if(pre == null || root.val != pre.val){
            count = 1;
        }else{
            count++;
        }
        if(count == maxcount){
            list.add(root.val);
        }
        if(count > maxcount){
            list.clear();
            list.add(root.val);
            maxcount = count;
        }
        pre = root;
        inOrder(root.right);
    }

}

 

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

难题 

 首先找到公共祖先肯定要自底向上查找 需要用到后序遍历

递归函数需要有返回值 来判断是否找到符合要求的节点

在递归函数有返回值的情况下:如果要搜索一条边,递归函数返回值不为空的时候,立刻返回,如果搜索整个树,直接用一个变量left、right接住返回值,这个left、right后序还有逻辑处理的需要,也就是后序遍历中处理中间节点的逻辑(也是回溯)

 

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q){
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left, p , q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

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

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值