mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-10 20:04:44 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -34,7 +34,7 @@ class Solution {
|
||||
TreeNode cur = new TreeNode(-1);
|
||||
cur = root;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
//找到当前应该遍历的那个节点
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
@@ -47,7 +47,7 @@ class Solution {
|
||||
cur = temp.right;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -104,4 +104,4 @@ func inorderTraversal(root *TreeNode) []int {
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
###
|
||||
|
||||
@@ -49,10 +49,10 @@ class Solution {
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
if (root == null) return list;
|
||||
if (root == null) return list;
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode temp = stack.pop();
|
||||
TreeNode temp = stack.pop();
|
||||
if (temp.right != null) {
|
||||
stack.push(temp.right);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
我们知道后序遍历的顺序是,` 对于树中的某节点, 先遍历该节点的左子树, 再遍历其右子树, 最后遍历该节点`。
|
||||
|
||||
那么我们如何利用栈来解决呢?
|
||||
那么我们如何利用栈来解决呢?
|
||||
|
||||
我们直接来看动画,看动画之前,但是我们`需要带着问题看动画`,问题搞懂之后也就搞定了后序遍历。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user