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 {
public int minDiffInBST(TreeNode root) {
int min = Integer.MAX_VALUE;
TreeNode pre = null;
Deque<TreeNode> stk = new LinkedList<>();
while (root != null || !stk.isEmpty()) {
TreeNode temp = null;
Deque<TreeNode> deque = new LinkedList<>();
while (root != null || !deque.isEmpty()) {
while (root != null) {
stk.offerLast(root);
deque.offerLast(root);
root = root.left;
}
root = stk.pollLast();
if (pre != null) {
min = Math.min(min, root.val - pre.val);
root = deque.pollLast();
if (temp != null) {
min = Math.min(min, root.val - temp.val);
}
pre = root;
temp = root;
root = root.right;
}
return min;

File diff suppressed because one or more lines are too long