783. Minimum Distance Between BST Nodes - Easy

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

Example :

Input: root = [4,2,6,1,3,null,null]
Output: 1
Explanation:
Note that root is a TreeNode object, not an array.

The given tree [4,2,6,1,3,null,null] is represented by the following diagram:

          4
        /         2      6
     / \
    1   3  

while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.

Note:

  1. The size of the BST will be between 2 and 100.
  2. The BST is always valid, each node‘s value is an integer, and each node‘s value is different.

M1: inorder traversal (recursive)

time: O(n), space: O(h)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int prev = Integer.MAX_VALUE, minDist = Integer.MAX_VALUE;

    public int minDiffInBST(TreeNode root) {
        if(root == null) {
            return 0;
        }
        helper(root);
        return minDist;
    }

    public void helper(TreeNode node) {
        if(node == null) {
            return;
        }
        helper(node.left);
        if(prev != Integer.MAX_VALUE) {
            minDist = Math.min(minDist, Math.abs(node.val - prev));
        }
        prev = node.val;
        helper(node.right);
    }
}

M2: inorder traversal (iterative)

time: O(n), space: O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDiffInBST(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int minDist = Integer.MAX_VALUE;
        LinkedList<TreeNode> s = new LinkedList<>();
        TreeNode cur = root;
        int prev = Integer.MAX_VALUE;
        while(cur != null || !s.isEmpty()) {
            while(cur != null) {
                s.offerFirst(cur);
                cur = cur.left;
            }

            cur = s.pollFirst();
            if(prev != Integer.MAX_VALUE) {
                minDist = Math.min(minDist, Math.abs(prev - cur.val));
            }
            prev = cur.val;
            cur = cur.right;
        }
        return minDist;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10251061.html

时间: 2024-07-29 21:15:04

783. Minimum Distance Between BST Nodes - Easy的相关文章

leetcode 783. Minimum Distance Between BST Nodes ---中序遍历

过年晚上无聊,233333333 题解: BST树的中序遍历是有序的,遍历过程中,记录前一个值,然后和当前值比较,来更新最小的minimum distance 注意python参数传递时候,像list这些object是传引用,单独int的数是传值的 void getans(TreeNode* root,int &pre,int &ans) { if(root==NULL) return; getans(root->left,pre,ans); if(pre!=INT_MAX) ans

783. Minimum Distance Between BST Nodes BST节点之间的最小距离

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

783. Minimum Distance Between BST Nodes

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

[LeetCode&amp;Python] Problem 783. Minimum Distance Between BST Nodes

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意: root 是树结点对象 (TreeNode object),而不是数组. 给定的树 [4,2,6,1,3,null,null] 可表示为下图: 4 / 2 6 / \ 1 3 最小的差

LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个不同节点的值之间的最小差值.示例: 给定的树[4,2,6,1,3,null,null]由下图表示: 4 / 2 6 / \ 1 3 输出:1 说明:请注意,root是TreeNode对象,而不是数组.该树中的任意节点最小差值为1,它发生在节点1和节点2之间,也发生在节点3和节点2之间. 注意: BS

[leetcode-783-Minimum Distance Between BST Nodes]

Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree. Example : Input: root = [4,2,6,1,3,null,null] Output: 1 Explanation: Note that root is a TreeNode objec

【leetcode】1320. Minimum Distance to Type a Word Using Two Fingers

题目如下: You have a keyboard layout as shown above in the XY plane, where each English uppercase letter is located at some coordinate, for example, the letter A is located at coordinate (0,0), the letter B is located at coordinate (0,1), the letter P is

2017南宁网络赛 Problem J Minimum Distance in a Star Graph ( 模拟 )

题意 : 乱七八糟说了一大堆,实际上就是问你从一个序列到另个序列最少经过多少步的变化,每一次变化只能取序列的任意一个元素去和首元素互换 分析 : 由于只能和第一个元素去互换这种操作,所以没啥最优的特别方法,只要乖乖模拟即可,如果第一个元素不在正确位置则将它和正确位置的元素交换使其回到正确位置,如果第一个元素的位置就是正确的,那么就往后找不在正确位置的元素将它互换到第一个去,然后再对第一个元素进行判断即可,直到序列变成最终满足条件的序列........ #include<bits/stdc++.h