给定一个二叉树,返回它的 前序 遍历。

 示例:

输入: [1,null,2,3]  1\2/3 输出: [1,2,3]

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

">

给定一个二叉树,返回它的 前序 遍历。

示例:

输入: [1,null,2,3]  1\2/3 输出: [1,2,3]

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

迭代

把之前写的迭代版前序遍历扒出来抄一抄。。。。

class Solution:def preorderTraversal(self, root: TreeNode) -> List[int]:stack, ans = [root], []while stack:tmp = stack.pop()if tmp:ans.append(tmp.val)if tmp.right is not None:stack.append(tmp.right)if tmp.left is not None:stack.append(tmp.left)return ans

144. Binary Tree Preorder Traversal 二叉树的前序遍历相关推荐

  1. LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)

    问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...

  2. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* ...

  3. 【二叉树迭代版前序遍历】LeetCode 144. Binary Tree Preorder Traversal

    LeetCode 144. Binary Tree Preorder Traversal Solution1:递归版 二叉树的前序遍历递归版是很简单的,前序遍历的迭代版相对是最容易理解的. 迭代版链接 ...

  4. [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal

    66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...

  5. leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  6. 144. Binary Tree Preorder Traversal(非递归实现二叉树的前序遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  7. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  8. [LeetCode] 144. Binary Tree Preorder Traversal Java

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...

  9. Leetcode - 144. Binary Tree Preorder Traversal (层次遍历)

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

最新文章

  1. Java判断文本文件编码格式以及读取
  2. adobe aem_AEM中的单元测试(大声思考)
  3. “你要是有这个功能就好了!”
  4. IDEA新建maven项目漏掉webapp目录解决方法
  5. 聊聊高并发(三十五)Java内存模型那些事(三)理解内存屏障
  6. Java实现递归回溯,解决八皇后问题,数据结构与算法
  7. 理论基础 —— 线性表 —— 循环链表
  8. CLR的程序集定位算法(转)
  9. python实现模糊搜索_Python 代码实现模糊查询
  10. SQL Server 表中有重复的数据,无法对重复数据进行更新或删除。
  11. KVM/QEMU桥接网络设置[zz]
  12. opencv常用函数整理(一)
  13. Visio图片导出不清晰
  14. CSDN目前有多少注册用户数?
  15. 基于ATmega16单片机 程控滤波器程序源代码
  16. DEP和ASLR的原理与破解
  17. 【术语】本地部署、云化部署、混合部署
  18. 从零开始构建自己的WebGL3D引擎---思考与设计
  19. 折纸问题(规律题)(map的使用)
  20. Excel VBA 金融建模 培训

热门文章

  1. 倒影问题(reflect:below)
  2. 网络编程第三讲UDP编写
  3. python---django中权限框架设计
  4. PAT 1079. Total Sales of Supply Chain
  5. linux函数的阻塞与非阻塞IO及错误处理
  6. 透明色代码是多少_北京宣传画册印刷多少钱
  7. JAVASCRIPT发展历程
  8. 求和第一个算到最后一个周末求_Excel最全求和函数—sum家族系列
  9. Java黑皮书课后题第3章:3.14(游戏:猜硬币的正反面)编写程序,让用户猜一猜是硬币的正面还是反面。随即产生一个整数0或1,分别表示
  10. mysql got signal 11_mysql bug : mysqld got signal 11