https://leetcode.com/problems/binary-tree-inorder-traversal/
递归一般写起来比较方便,使用循环和递归一般是比较常规的写法。一般写程序递归要避免,但是对于二叉树来说,递归的写法实在太过方便。可以看看它的答案,有3中写法。

func inorderTraversal(_ root: TreeNode?) -> [Int] {
    guard let r = root else { return [] }
    return inorderTraversal(r.left) + [r.val] + inorderTraversal(r.right)
}

Runtime: 8 ms, faster than 100.00% of Swift online submissions for Binary Tree Inorder Traversal.
Memory Usage: 19 MB, less than 40.00% of Swift online submissions for Binary Tree Inorder Traversal.