Skip to content

Commit da6ec4e

Browse files
committed
添加 Swift 代码实现
1 parent acc662b commit da6ec4e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

animation-simulation/二叉树/二叉树中序遍历(Morris).md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,34 @@ class Solution {
102102
}
103103
```
104104

105+
Swift Code:
106+
107+
```swift
108+
class Solution {
109+
func inorderTraversal(_ root: TreeNode?) -> [Int] {
110+
var list:[Int] = []
111+
guard root != nil else {
112+
return list
113+
}
114+
var p1 = root, p2: TreeNode?
115+
while p1 != nil {
116+
p2 = p1!.left
117+
if p2 != nil {
118+
while p2!.right != nil && p2!.right !== p1 {
119+
p2 = p2!.right
120+
}
121+
if p2!.right == nil {
122+
p2!.right = p1
123+
p1 = p1!.left
124+
continue
125+
} else {
126+
p2!.right = nil
127+
}
128+
}
129+
list.append(p1!.val)
130+
p1 = p1!.right
131+
}
132+
return list
133+
}
134+
}
135+
```

0 commit comments

Comments
 (0)