代码随想录算法训练营第二十一天| LeetCode530.二叉搜索树的最小绝对差、LeetCode501.二叉搜索树中的众数、LeetCode236. 二叉树的最近公共祖先

本文介绍了如何使用递归和双指针方法解决LeetCode题目530(最小绝对差)、501(二叉搜索树中的众数)以及236(最近公共祖先),展示了在二叉搜索树中利用中序和后序遍历来解决问题的技巧。
摘要由CSDN通过智能技术生成

#LeetCode 530. Minimum Absolute Difference in BST

#LeetCode 530. 视频讲解:二叉搜索树中,需要掌握如何双指针遍历!| LeetCode:530.二叉搜索树的最小绝对差_哔哩哔哩_bilibili

定义一个新的函数时,考虑返回值类型。如果需要返回值,那么说明这个函数需要返回某个特性、特点、数值。如果不需要遍历整个二叉树,只需要某个特定路径或者某个节点。在本题中,定义了一个全局变量记录了minimal value,所以不需要函数具有返回值。

递归法(使用双指针)代码:

/**
 * 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) {
        traversal(root);
        return result;
    }
    public void traversal(TreeNode cur) {
        if (cur == null) {
            return;
        }
        traversal(cur.left);
        if(pre != null) {
            result = Math.min(result, cur.val - pre.val);
        }
        pre = cur;
        traversal(cur.right);
    }
}

#LeetCode 501. Find Mode in Binary Search Tree

#LeetCode 501. 视频讲解:不仅双指针,还有代码技巧可以惊艳到你! | LeetCode:501.二叉搜索树中的众数_哔哩哔哩_bilibili

建立全局变量:pre 指针(指向当前节点的前一个节点)、maxCount 变量(记录目前遍历到的最大数目)、count (记录当前节点的重复数目)、ArrayList 类型的result(由于最大重复的数目不知道,所以设置可变长度的ArrayList 类型,在return 之前将ArrayList 转换为int 数组)。如果看到是二叉搜索树,那么考虑用中序遍历。

迭代法代码:

/**
 * 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;
    ArrayList<Integer> result;
    TreeNode pre = null;
    public int[] findMode(TreeNode root) {
        result = new ArrayList<>();
        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 (pre.val == cur.val) {
            count += 1;
        }
        else {
            count = 1;
        }
        pre = cur;
        if (maxCount == count) {
            result.add(cur.val);
        }
        if (maxCount < count) {
            maxCount = count;
            result.clear();
            result.add(cur.val);
        }
        traversal(cur.right);
    }
}

#LeetCode 236. Lowest Common Ancestor of a Binary Tree

#LeetCode 236. 视频讲解:自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先_哔哩哔哩_bilibili

使用后序遍历,左右中的“中”体现了回溯,是通过左右子树的返回值,处理中节点。递归的终止条件是遇到了节点p 或者节点q ,则向上返回。本题的巧妙在于如果设置的left 节点变量为空,right 节点变量不为空,就返回right ,说明目标节点是通过right 返回的。如果设置的right 节点变量为空,left 节点变量不为空,就返回left ,说明目标节点是通过left 返回的。如果两个节点都为空,则返回left 或者right 都可以,或者直接返回null。

迭代 + 回溯代码:

/**
 * 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;
        }
        if (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 root;
        }
        if (left != null && right == null) {
            return left;
        } else if (left == null && right != null) {
            return right;
        } else return null;
    }
}
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值