diff --git a/animation-simulation/二叉树/二叉树中序遍历(迭代).md b/animation-simulation/二叉树/二叉树中序遍历(迭代).md index adbec99..8dd15e7 100644 --- a/animation-simulation/二叉树/二叉树中序遍历(迭代).md +++ b/animation-simulation/二叉树/二叉树中序遍历(迭代).md @@ -51,5 +51,32 @@ class Solution { } ``` +Swift Code: + +```swift +class Solution { + func inorderTraversal(_ root: TreeNode?) -> [Int] { + var arr:[Int] = [] + var cur = root + var stack:[TreeNode] = [] + + while !stack.isEmpty || cur != nil { + //找到当前应该遍历的那个节点 + while cur != nil { + stack.append(cur!) + cur = cur!.left + } + //此时指针指向空,也就是没有左子节点,则开始执行出栈操作 + if let temp = stack.popLast() { + arr.append(temp.val) + //指向右子节点 + cur = temp.right + } + } + return arr + } +} +``` + ###