Skip to content

Latest commit

 

History

History
103 lines (73 loc) · 1.96 KB

0144-binary-tree-preorder-traversal.adoc

File metadata and controls

103 lines (73 loc) · 1.96 KB

144. Binary Tree Preorder Traversal

{leetcode}/problems/binary-tree-preorder-traversal/[LeetCode - Binary Tree Preorder Traversal^]

Given a binary tree, return the preorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,2,3]

Follow up: Recursive solution is trivial, could you do it iteratively?

解题分析

凡是用递归能解决的问题,都可以使用遍历来解决。用递归来求解问题,无非就是使用了方法栈来保存相关信息。同样,可以使用 Stack 来自己动手维护这些信息。

link:{sourcedir}/_0144_BinaryTreePreorderTraversal.java[role=include]
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Recur.java[role=include]
一刷
link:{sourcedir}/_0144_BinaryTreePreorderTraversal.java[role=include]
二刷
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Recur.java[role=include]
二刷(Morris遍历)
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Morris.java[role=include]

迭代实现

{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
{image_attr}
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Stack.java[role=include]