刷题第21天 | 530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先

530. Minimum Absolute Difference in BST

题目链接:530. Minimum Absolute Difference in BST
思路链接:代码随想录二叉树-二叉搜索树的最小绝对差

思路

这题利用中序遍历就能从小到大有序地遍历二叉搜索树了,然后再定义pre和curr两个指针,得到两个指针指向的节点值之差的最小值即可。

Code

/**
 * 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 {
    // 递归法 + 双指针法
    // 中序遍历
    // 通过中序遍历可以得到一个有序数组,然后通过双指针就能得到最小值了
    private int min = Integer.MAX_VALUE;
    private TreeNode pre = null;
    private void traversal(TreeNode curr) {
        if (curr == null) {
            return;
        }
        if (curr.left != null) {
            traversal(curr.left);
        }
        if (pre != null) {
            min = Math.min(min, curr.val - pre.val);
        }
        pre = curr;
        if (curr.right != null) {
            traversal(curr.right);
        }
    }
    
    public int getMinimumDifference(TreeNode root) {
        traversal(root);
        return min;
    }
}

501. Find Mode in Binary Search Tree

题目链接:501. Find Mode in Binary Search Tree
思路链接:代码随想录二叉树-二叉搜索树中的众数

思路

若要利用二叉搜索树的性质,必须要用中序遍历。利用双节点来更新count。利用另外定义的count以及maxCount来记录元素出现的个数以及当前出现元素的最大个数。无论何时,如果count等于maxCount就将当前的节点值记录下来(利用某种容器),不管当前节点是否就是众数。一旦count大于maxCount(maxCount需要更新),就清空容器,再将当前的节点值记录下来。

Code

/**
 * 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.Set;
import java.util.HashSet;

class Solution {
    // 递归法 + 双指针法
    // 利用二叉搜索树的性质一定要用中序遍历
    // 不需要像暴力解法一样遍历两边二叉树,使用trick来操作count和maxCount就能达到只遍历一遍的目的
    private TreeNode pre = null;
    private int count = 1;
    private int maxCount = 1;
    private Set<Integer> set = new HashSet<>();
    private void traversal(TreeNode curr) {
        // 结束条件
        if (curr == null) {
            return ;
        }
        // 左
        traversal(curr.left);
        // 中
        // 如果pre在正常遍历时
        if (pre != null) {
            // 如果pre与curr相等
            if (pre.val == curr.val) {
                count++;
            } else { // 一旦不同,就重置count
                count = 1;
            }
            // 如果count与maxCount相等就在set里加入值(这里不排除当前maxCount不为实际最大count的情况)
            if (count == maxCount) {
                set.add(curr.val);
            } else if (count > maxCount) { // 如果发现maxCount不为实际最大count,那么就重置set,重新加入值
                set.clear();
                maxCount = count;
                set.add(curr.val);
            }
        } else { // 如果curr还在root节点,pre还没赋值时
            set.add(curr.val);
        }
        pre = curr;
        // 右
        traversal(curr.right);
    }
    
    public int[] findMode(TreeNode root) {
        traversal(root);
        int[] result = new int[set.size()];
        int index = 0;
        for (int num : set) {
            result[index++] = num;
        }
        return result;
    }
}

236. Lowest Common Ancestor of a Binary Tree

题目链接:236. Lowest Common Ancestor of a Binary Tree
思路链接:代码随想录二叉树-二叉树的最近公共祖先

思路

要想得到公共祖先,应该从二叉树的底下从下往上遍历,所以必须要用后续遍历来达到回溯的目的。只要子节点不等于p或q,就想上返回null;如果子节点等于p或q,就向上返回相应的值。如果左右节点分别为p和q,那么就返回当前节点。遍历整棵树。

Code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    // 递归法
    // 利用后续遍历就能利用回溯了
    // 从树的最深处开始向上返回,如果叶子节点不等于p或q,那么返回null;子节点如果等于p或q,就返回相应的值;如果两个字节点一个等于p,一个等于q,那么返回母节点
    // 回溯会将底下的值一直返回到最上面的那一层
    private TreeNode traversal(TreeNode curr, TreeNode p, TreeNode q) {
        // 结束条件
        if (curr == null) {
            return null;
        }
        if (curr.val == p.val || curr.val == q.val) {
            return curr;
        }
        // 左 
        // 因为需要遍历整棵树,所以需要将返回值储存在变量内,之后需要使用该变量进行逻辑操作
        TreeNode left = traversal(curr.left, p, q);
        // 右
        // 因为需要遍历整棵树,所以需要将返回值储存在变量内,之后需要使用该变量进行逻辑操作
        TreeNode right = traversal(curr.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 curr;
        }
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        return traversal(root, p, q);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值