Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes’ values.
(先序遍历二叉树)
Follow up: Recursive solution is trivial, could you do it iteratively?
Example:
1. 递归
具体实现方法如下:
1 | # Definition for a binary tree node. |
2. 迭代 & 栈
先将访问根节点,然后将右子树节点加入到栈中,然后访问左子树。具体实现方法如下:
1 | # Definition for a binary tree node. |
1 | class Solution: |