Given a binary search tree with non-negative values, find the minimum absolute differencebetween values of any two nodes.
这个题和BST中最小距离没有区别
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int getMinimumDifference(TreeNode root) {
if(root == null)
return 0;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
while(p != null) {
stack.push(p);
p = p.left;
}
int res = Integer.MAX_VALUE;
Integer last = null;
while(stack.size() > 0) {
TreeNode temp = stack.pop();
if(last == null) {
last = temp.val;
} else {
res = Math.min(res, temp.val - last);
last = temp.val;
}
if(temp.right != null) {
p = temp.right;
while(p != null) {
stack.push(p);
p = p.left;
}
}
}
return res;
}
}