第六章 二叉树part07

122 阅读1分钟

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.

题目解析:

  • 其实就是按值的顺序比较相邻两个节点的差(双指针),可以使用中序遍历

代码:

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

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.

题目解析:

  • 中序双指针找到众数

代码:

lass Solution {
    TreeNode prev;
    int count = 0, maxCount = 0;
    public int[] findMode(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        dfs(root, result);
        return result.stream().mapToInt(Integer::intValue).toArray();
    }

    public void dfs(TreeNode root, List<Integer> result) {
        if (root == null) return;
        dfs(root.left, result);
        if (prev == null) {
            count++;
        } else {
            if (prev.val == root.val) {
                count++;
            } else {
                count = 1;
            }
        }
        if (count >= maxCount) {
            if (count > maxCount) {
                result.clear();
                maxCount = count;
            }
            result.add(root.val);
        } 
        prev = root;
        dfs(root.right, result);
    }
}

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).”

题目解析:

  • 只要节点等于P或者q就返回该节点,如果左右节点均不为null,说明左右节点为p,q,则该节点为目标节点。(节点的值是unique)

代码:

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