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:

530. Minimum Absolute Difference in BST_第1张图片
Input: root = [4,2,6,1,3]
Output: 1

Example 2:

530. Minimum Absolute Difference in BST_第2张图片
Input: root = [1,0,48,null,null,12,49]
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [2, 104].
  • 0 <= Node.val <= 105

Note: This question is the same as 783: Minimum Distance Between BST Nodes - LeetCode


这题求BST中任意两个子节点的最小差。其实跟上一题挺像的,也是需要记录全局的最小和上一次遍历的节点,利用BST的性质(inorder)进行递归。然而自己写的时候出现了以下几个问题:

1. if (root == null)时,需要return min,刚开始直接return 0了……

2. 刚开始写成了直接return Math.min(f(left), f(right)),submit完发现WA了,看了答案才发现需要严格进行inorder traversal,我这直接写成preorder了无法preserve order。

3. 另外也看到有人指出用全局变量其实不太好,如果要改成传函数参数的话,需要参数是mutable的,方便一边遍历一边更新。可以参考这个,用长度为1的array来保存:Java O(n) Time Inorder Traversal Solution - Minimum Absolute Difference in BST - LeetCode

/**
 * 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 prev = null;
    int min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return min;
        }
        min = getMinimumDifference(root.left);
        if (prev != null) {
            min = Math.min(min, Math.abs(root.val - prev.val));
        }
        prev = root;
        min = getMinimumDifference(root.right);
        return min;
    }
}

递归:

其实就是inorder的递归方法,which早就忘了,这次就复习了一下。刚开始写成preorder了看了半天都不对……以及还是得用到prev啊。

/**
 * 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 prev = null;
    int min = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return min;
        }
        Deque stack = new ArrayDeque<>();
        pushAllLeftNodes(root, stack);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (prev != null) {
                min = Math.min(min, Math.abs(node.val - prev.val));
            }
            pushAllLeftNodes(node.right, stack);
            prev = node;
        }
        return min;
    }

    private void pushAllLeftNodes(TreeNode node, Deque stack) {
        while (node != null) {
            stack.push(node);
            node = node.left;
        }
    }
}

 

你可能感兴趣的:(LeetCode,leetcode)