783:二叉搜索树节点最小距离

This commit is contained in:
huangge1199 2021-04-14 09:10:34 +08:00
parent a8e44be0c1
commit 32e88e7d8a
2 changed files with 9 additions and 9 deletions

View File

@ -67,18 +67,18 @@ public class MinimumDistanceBetweenBstNodes {
class Solution { class Solution {
public int minDiffInBST(TreeNode root) { public int minDiffInBST(TreeNode root) {
int min = Integer.MAX_VALUE; int min = Integer.MAX_VALUE;
TreeNode pre = null; TreeNode temp = null;
Deque<TreeNode> stk = new LinkedList<>(); Deque<TreeNode> deque = new LinkedList<>();
while (root != null || !stk.isEmpty()) { while (root != null || !deque.isEmpty()) {
while (root != null) { while (root != null) {
stk.offerLast(root); deque.offerLast(root);
root = root.left; root = root.left;
} }
root = stk.pollLast(); root = deque.pollLast();
if (pre != null) { if (temp != null) {
min = Math.min(min, root.val - pre.val); min = Math.min(min, root.val - temp.val);
} }
pre = root; temp = root;
root = root.right; root = root.right;
} }
return min; return min;

File diff suppressed because one or more lines are too long