Skip to content

Files

Latest commit

May 9, 2025
c53dd60 · May 9, 2025

History

History
125 lines (79 loc) · 2.62 KB

0144-binary-tree-preorder-traversal.adoc

File metadata and controls

125 lines (79 loc) · 2.62 KB

144. 二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

示例 1:

输入:root = [1,null,2,3]

输出:[1,2,3]

解释:

{image_attr}

示例 2:

输入:root = [1,2,3,4,5,null,8,null,null,6,7,9]

输出:[1,2,4,5,6,7,3,8,9]

解释:

{image_attr}

示例 3:

输入:root = []

输出:[]

示例 4:

输入:root = [1]

输出:[1]

提示:

  • 树中节点数目在范围 [0, 100]

  • -100 <= Node.val <= 100

进阶:递归算法很简单,你可以通过迭代算法完成吗?

思路分析

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

{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.java[role=include]
一刷(迭代)
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Stack.java[role=include]
二刷
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Recur.java[role=include]
三刷(Morris遍历)
link:{sourcedir}/_0144_BinaryTreePreorderTraversal_Morris.java[role=include]