Day21(530, 501, 236)

这篇文章介绍了三个关于二叉搜索树(BST)的问题:找到树中任意两个节点的最小绝对差值,查找BST中的最频繁出现的元素(模式),以及寻找给定节点的最低公共祖先。提供的解决方案分别通过遍历BST来实现,包括前序遍历和递归方法。
摘要由CSDN通过智能技术生成

530. Minimum Absolute Difference in BST

Given the root of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.

Example 1:

Input: root = [4,2,6,1,3]
Output: 1

Example 2:

Input: root = [1,0,48,null,null,12,49]
Output: 1

class Solution {  
    public int min = Integer.MAX_VALUE;  
    public TreeNode pre;  
  
    public void traversal(TreeNode cur) {  
        if (cur == null) return;  
        traversal(cur.left);  
        if (pre != null) {  
            min = Math.min(min, cur.val-pre.val);  
        }  
        pre = cur;  
        traversal(cur.right);  
    }  
  
    public int getMinimumDifference(TreeNode root) {  
        traversal(root);  
        return min;  
    }  
}

501. Find Mode in Binary Search Tree

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input: root = [1,null,2,2]
Output: [2]

Example 2:

Input: root = [0]
Output: [0]

class Solution {  
    public TreeNode pre;  
    public int count = 0;  
    public int maxCount = 0;  
    public List<Integer> result = new ArrayList<>();  
    public void BST(TreeNode cur) {  
        if (cur == null) return;  
        BST(cur.left);  
        if (pre == null) count = 1;  
        else if (pre.val == cur.val) count++;  
        else count = 1;  
        pre = cur;  
        if (count == maxCount) result.add(cur.val);  
  
        if (count > maxCount) {  
            maxCount = count;  
            result.clear();  
            result.add(cur.val);  
        }  
        BST(cur.right);  
    }  
  
    public int[] findMode(TreeNode root) {  
        BST(root);  
        int size = result.size();  
        int[] arr = new int[size];  
        for (int i = 0; i < size; i++) {  
            arr[i] = result.get(i);  
        }  
        return arr;  
    }  
}

236. Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

class Solution {  
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {  
        if (root == null || root == p || root == q) return root;  
        TreeNode nodeLeft = lowestCommonAncestor(root.left, p, q);  
        TreeNode nodeRight = lowestCommonAncestor(root.right, p, q);  
        if (nodeLeft != null && nodeRight != null) return root;  
        if (nodeLeft != null) return nodeLeft;  
        return nodeRight;  
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值