783:二叉搜索树节点最小距离
This commit is contained in:
parent
a8e44be0c1
commit
32e88e7d8a
@ -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
Loading…
Reference in New Issue
Block a user