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?

树的遍历,最常见的有先序遍历,中序遍历,后序遍历和层序遍历,它们用递归实现起来都非常的简单。而题目的要求是不能使用递归求解。

1. 用迭代和stack。2. Morris Traversal Solution

Python: Stack,  Time: O(n), Space: O(h) # h is the height of the tree

class Solution2(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result, stack = [], [(root, False)]
while stack:
root, is_visited = stack.pop()
if root is None:
continue
if is_visited:
result.append(root.val)
else:
stack.append((root.right, False))
stack.append((root.left, False))
stack.append((root, True))
return result

Python: Morris, Time: O(n), Space: O(1)

class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result, curr = [], root
while curr:
if curr.left is None:
result.append(curr.val)
curr = curr.right
else:
node = curr.left
while node.right and node.right != curr:
node = node.right if node.right is None:
result.append(curr.val)
node.right = curr
curr = curr.left
else:
node.right = None
curr = curr.right return result  

类似题目:

[LeetCode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历  

[LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历的更多相关文章

  1. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  2. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

  3. Leetcode 144 Binary Tree Preorder Traversal 二叉树

    二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...

  4. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

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

  5. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  6. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  7. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  8. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  9. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

随机推荐

  1. Django设置应用名与模型名为中文

    修改polls包里面的apps.py: from django.apps import AppConfig class PollsConfig(AppConfig): name = 'polls' v ...

  2. windows下面,PHP如何启动一些扩展功能

    我今天在试这个时,发现php有些默认设置,是需要人为介入修改的. 比如,当我们在安装一个软件,而这个软件需要启用php一些扩展功能. 那么,按一般套路,将php.ini文件里的相关行的注释去掉即可. ...

  3. 【云栖社区002-二分估值法】要求不用数学库,求 sqrt (2)精确到小数点后10位(Java版)

    如题 初步审题的时候,想到的是暴力搜索:初步设置一个合法的种子,依次按照1e-2,1e-3,1e-4,1e-5,1e-6 , 1e-7...暴力搜索,额,就是太麻烦了. 打比赛搜索写多了,一看见题目就 ...

  4. 数组函数some、every、find、filter、map、forEach有什么区别

  5. C# 获取操作系统空闲时间

    获取系统鼠标和键盘没有任何操作的空闲时间 public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// ...

  6. DT6.0开发之-调用信息评论

    今天在做destoon6.0产品内页开发时候,用到了调取当前信息评论,所以就顺便做下笔记. 调用当前信息的评论代码: <!--{tag("table=destoot_comment&a ...

  7. Git的个人总结

    Git Git简史: 同生活中的许多伟大事物一样,Git 诞生于一个极富纷争大举创新的年代. Linux 内核开源项目有着为数众多的参与者. 绝大多数的 Linux 内核维护工作都花在了提交补丁和保存 ...

  8. AJax的三种响应

    AJax的响应 1.普通文本方式(字符串) resp.getWriter().print("你好"); 2.JSON格式当要给前台页面传输 集合或者对象时 使用普通文本传输的时St ...

  9. php web开发——文件夹的上传和下载

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  10. bootstrap导航条组件

    一.导航条模板(官方文档) <nav class="navbar navbar-default"> <div class="container-flui ...