leet-code/src/main/java/leetcode/editor/cn/MinimumDistanceBetweenBstNodes.java
2021-04-29 23:21:52 +08:00

89 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
//
// 注意:本题与 530https://leetcode-cn.com/problems/minimum-absolute-difference-in-bs
//t/ 相同
//
//
//
//
//
// 示例 1
//
//
//输入root = [4,2,6,1,3]
//输出1
//
//
// 示例 2
//
//
//输入root = [1,0,48,null,null,12,49]
//输出1
//
//
//
//
// 提示:
//
//
// 树中节点数目在范围 [2, 100] 内
// 0 <= Node.val <= 105
//
//
//
// Related Topics 树 深度优先搜索 递归
// 👍 127 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.*;
//783:二叉搜索树节点最小距离
public class MinimumDistanceBetweenBstNodes {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumDistanceBetweenBstNodes().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 {
public int minDiffInBST(TreeNode root) {
int min = Integer.MAX_VALUE;
TreeNode temp = null;
Deque<TreeNode> deque = new LinkedList<>();
while (root != null || !deque.isEmpty()) {
while (root != null) {
deque.offerLast(root);
root = root.left;
}
root = deque.pollLast();
if (temp != null) {
min = Math.min(min, root.val - temp.val);
}
temp = root;
root = root.right;
}
return min;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}