Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree
Given binary tree
{1,#,2,3}
,1 \ 2 / 3
return
[1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
用stack做,res存结果,不要混淆,那么我们要的是res的顺序是preorder, 那么stack是一个缓冲,root, left, right, 那么stack是root, right, left, 用一个while循环来模拟.
No comments:
Post a Comment