[leetcode] Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? tag: stack 不能再简单的题了,先序遍历。 即使说不让用递归,也可以很好的解出来。做此题时需要知道一个思想,递归的本质其实就是进程内存中维护着一个栈。这在计算机系统概论,也就是Patt上的那门课上,他着重解释过。所以如果不让我们用递归的话,那就加个栈吧! 为了比较速度,我把两种方法都贴上。应该是使用栈的方法更快。 版本一:使用递归 /** * Definition for binary […]