We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ef44b3f commit 7572d7cCopy full SHA for 7572d7c
animation-simulation/二叉树/二叉树中序遍历(迭代).md
@@ -51,5 +51,32 @@ class Solution {
51
}
52
```
53
54
+Swift Code:
55
+
56
+```swift
57
+class Solution {
58
+ func inorderTraversal(_ root: TreeNode?) -> [Int] {
59
+ var arr:[Int] = []
60
+ var cur = root
61
+ var stack:[TreeNode] = []
62
63
+ while !stack.isEmpty || cur != nil {
64
+ //找到当前应该遍历的那个节点
65
+ while cur != nil {
66
+ stack.append(cur!)
67
+ cur = cur!.left
68
+ }
69
+ //此时指针指向空,也就是没有左子节点,则开始执行出栈操作
70
+ if let temp = stack.popLast() {
71
+ arr.append(temp.val)
72
+ //指向右子节点
73
+ cur = temp.right
74
75
76
+ return arr
77
78
+}
79
+```
80
81
###
82
0 commit comments