代码随想录|二叉树|leetcode530,501,236

二叉搜索树的最小绝对差 

/**
 * 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 {
    TreeNode pre = null;
    int result = Integer.MAX_VALUE;//为了承接差值的最小值
    public int getMinimumDifference(TreeNode root) {
        if(root == null) return 0;
        traversal(root);
        return result;

    }

    public void traversal(TreeNode root){
        //左
        if(root == null) return;
        traversal(root.left);
        //中
    
        if(pre != null){
            result = Math.min(result, root.val - pre.val);
        }
        pre = root;
        //右
        traversal(root.right);

    }
}

思路:

  1. 中序遍历:由于BST的性质(左子树节点的值 < 根节点的值 < 右子树节点的值),中序遍历将按升序顺序输出所有节点的值。
  2. 维护前一个节点:在中序遍历时,我们维护一个前一个遍历节点pre。对于当前遍历的节点,与pre节点的差值可能是最小绝对差值的候选。
  3. 更新结果:对于每一个非首节点,计算与前一个节点的差,并尝试用它更新result

二叉搜索树中的众数 

/**
 * 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 {
    ArrayList<Integer> resList;
    int maxCount;
    int count;
    TreeNode pre;
    public int[] findMode(TreeNode root) {
        resList = new ArrayList<>();
        maxCount = 0;
        count = 0;
        pre = null;
        findModel1(root);
        int [] res = new int[resList.size()];
        for(int i = 0; i < resList.size(); i++){
            res[i] = resList.get(i);
        }
        return res;

    }

    public void findModel1(TreeNode root){
        if(root == null){
            return;
        }
        //左
        findModel1(root.left);
        //中
        int rootValue = root.val;
        if(pre == null || rootValue != pre.val){
            count = 1;
        } else{
            count++;
        }
        if(count > maxCount){
            resList.clear();
            resList.add(rootValue);
            maxCount = count;
        }else if(count == maxCount){
            resList.add(rootValue);
        }
        pre = root;
        //中
        findModel1(root.right);


    }
}

 

思路: 

  1. 初始化:

    • resList:这是一个动态数组,用于保存所有出现次数最多的元素。
    • maxCount:当前已知的最大出现次数。
    • count:用于计算当前元素的出现次数。
    • pre:上一个遍历的节点。这个指针用于比较当前节点和上一个节点的值,以确定我们是否应该重置count
  2. 中序遍历:

    • findMode1函数是一个递归函数,用于中序遍历树。
    • 先遍历左子树,然后处理根节点,最后遍历右子树。
    • 对于每一个遍历到的节点,都会比较其值和pre节点的值:
      • 如果prenull(即这是遍历的第一个节点)或当前节点的值不等于pre的值,那么count被设置为1。
      • 否则,count增加1。
    • 接下来,我们将countmaxCount进行比较:
      • 如果count大于maxCount,我们清空resList,并将当前节点的值添加到其中,同时更新maxCount的值。
      • 如果count等于maxCount,则直接将当前节点的值添加到resList中。
    • 最后,将pre设置为当前节点,并遍历右子树。
  3. 结果转换:

    • resList转换为一个数组,并返回。

 二叉树的最近公共祖先

 

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 right;
        }else if(left != null && right == null){
            return left;
        }else{
            return root;
        }
        
    }
}

 思路:

  1. if(root == null || root == p || root == q):这个条件判断当前节点是否是null或者是否是我们要找的其中一个节点。如果是,则返回当前节点。

  2. 接下来,我们递归地在左子树和右子树中寻找pq

    • TreeNode left = lowestCommonAncestor(root.left, p, q);:在左子树中查找。

    • TreeNode right = lowestCommonAncestor(root.right, p, q);:在右子树中查找。

  3. 根据左右子树的返回值来确定最低公共祖先:

    • 如果leftright都为null,说明pq都不在当前节点的子树中,因此返回null

    • 如果leftnullright不为null,说明只有right子树中有pq,因此返回right

    • 如果left不为nullrightnull,说明只有left子树中有pq,因此返回left

    • 如果leftright都不为null,说明pq分别在当前节点的左右子树中,因此当前节点就是它们的最低公共祖先,返回root

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值