Problem: 530. 二叉搜索树的最小绝对差
文章目录
- 思路
- 复杂度
- Code
思路
👨🏫 录哥题解
复杂度
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
Code
// 递归
class Solution {int ans = Integer.MAX_VALUE;TreeNode pre;//一开始 pre = nullpublic int getMinimumDifference(TreeNode root){if (root == null)return 0;traversal(root);return ans;}private void traversal(TreeNode root){if (root == null)return;traversal(root.left);//左if (pre != null)//中ans = Math.min(ans, root.val - pre.val);pre = root;traversal(root.right);//右}
}