mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-06 23:41:36 +00:00
添加Go语言题解
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -78,4 +78,30 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func inorderTraversal(root *TreeNode) []int {
|
||||
res := []int{}
|
||||
if root == nil {
|
||||
return res
|
||||
}
|
||||
stk := []*TreeNode{}
|
||||
cur := root
|
||||
for len(stk) != 0 || cur != nil {
|
||||
// 找到当前应该遍历的那个节点,并且把左子节点都入栈
|
||||
for cur != nil {
|
||||
stk = append(stk, cur)
|
||||
cur = cur.Left
|
||||
}
|
||||
// 没有左子节点,则开始执行出栈操作
|
||||
temp := stk[len(stk) - 1]
|
||||
stk = stk[: len(stk) - 1]
|
||||
res = append(res, temp.Val)
|
||||
cur = temp.Right
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
|
Reference in New Issue
Block a user