Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func inorderTraversal(_ root: TreeNode?) -> [Int] { var list = [Int](), stack = [TreeNode](), p = root while p != nil || !stack.isEmpty { if let t = p { stack.append(t) p = t.left } else { let t = stack.popLast()! list.append(t.val) p = t.right } } return list } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼