about // doc

LeetCode - Binary Tree Preorder Traversal

Click here to go back to LeetCode summary page.

Problem description is here, or as follows:

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?

The definition of preorder traversal is illustrated as follows:

preorder traversal illustration

Comments on Solution 1, 2 and 3 are very similar to those in “Binary Tree Inorder Traversal” problem. See how Solution 2 here is different from the Solution 2 of “Binary Tree Inorder Traversal” and “Binary Tree Postorder Traversal” problems.

Again, Solution 1 and 2 should be memorized.

comments powered by Disqus