144 Binary Tree Preorder Traversal – Medium
Problem:
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?
Thoughts:
It is trivial to use recursion approach to solve this problem.
Preorder is to do a root, left, right.
Always append when meets a root. Then go deeper to the left subtree if got one.
Using a Stack is very helpful, which guaranteed all elements in the subtree will be appended prior to the right child.
Solutions:
Last updated