66: Binary tree preorder traversal
Given a binary tree
, return preorder traversal
of the tree
Note: write the
iterative
solution, therecursive
solutionis trivial
(see below)
public void PreorderTraversal(TreeNode root)
{
if (root == null)
{
return;
}
Console.WriteLine(root.Value);
PreorderTraversal(root.Left);
PreorderTraversal(root.Right);
}
Example 1
Input: 1
/ \
5 3
Output: [1, 5, 3]
Example 2
Input: 1
\
3
Output: [1, 3]
Helpful article