/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int min = Integer.MAX_VALUE ;
public int last = -1;
public void travel(TreeNode root){
if(root == null ){
return ;
}
travel(root.left);
if(last != -1){
min = Math.min(min , Math.abs(root.val - last));
}
last = root.val ;
travel(root.right);
}
public int getMinimumDifference(TreeNode root) {
travel(root);
return min;
}
}
530. Minimum Absolute Difference in BST
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- Given a binary search tree with non-negative values, find...
- 由于在中序遍历下的BST得到的值依然是依次有序的,利用这一点,保存前一个点和现有点进行比较就好了,时间是O(N)。...
- Given a binary search tree with non-negative values, find...
- 题目 Given a binary search tree with non-negative values, f...
- 题目 Given a binary search tree with non-negative values, f...