mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
新增java 統一迭代法-中序遍历
新增java 統一迭代法-中序遍历
This commit is contained in:
@ -174,6 +174,39 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
統一迭代法-中序遍历
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public int getMinimumDifference(TreeNode root) {
|
||||||
|
Stack<TreeNode> 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
|
```java
|
||||||
|
Reference in New Issue
Block a user