diff --git a/problems/0530.二叉搜索树的最小绝对差.md b/problems/0530.二叉搜索树的最小绝对差.md index fa1430de..cd24c6fa 100644 --- a/problems/0530.二叉搜索树的最小绝对差.md +++ b/problems/0530.二叉搜索树的最小绝对差.md @@ -174,6 +174,39 @@ class Solution { } } ``` +統一迭代法-中序遍历 +```Java +class Solution { + public int getMinimumDifference(TreeNode root) { + Stack stack = new Stack<>(); + TreeNode pre = null; + int result = Integer.MAX_VALUE; + + if(root != null) + stack.add(root); + while(!stack.isEmpty()){ + TreeNode curr = stack.peek(); + if(curr != null){ + stack.pop(); + if(curr.right != null) + stack.add(curr.right); + stack.add(curr); + stack.add(null); + if(curr.left != null) + stack.add(curr.left); + }else{ + stack.pop(); + TreeNode temp = stack.pop(); + if(pre != null) + result = Math.min(result, temp.val - pre.val); + pre = temp; + } + } + return result; + } +} +``` + 迭代法-中序遍历 ```java