Binary Tree Preorder Traversal

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?


Solution:

递归解法很简单:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param {TreeNode} root
# @return {integer[]}
def preorderTraversal(self, root):
if root == None:
return []
else:
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)

迭代版本也是常规的将递归改成迭代的版本:

用一个栈来模拟递归的过程,注意栈 FILO 的特点。所以,对于当前根,要把右子树先加入栈,然后再把左子树加入栈。

前序遍历的顺序是:根 - 左子树 - 右子树。

代码如下:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param {TreeNode} root
# @return {integer[]}
def preorderTraversal(self, root):
stack = []
path = []
if root != None:
stack.append(root)
while stack != []:
e = stack.pop()
path.append(e.val)
if e.right != None:
stack.append(e.right)
if e.left != None:
stack.append(e.left)
return path

【LeetCode】Binary Tree Preorder Traversal的更多相关文章

  1. 【leetcode】Binary Tree Preorder Traversal (middle)★

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

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

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

  3. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  4. 【Leetcode】【Medium】Binary Tree Preorder Traversal

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

  5. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  6. 【leetcode】Binary Tree Postorder Traversal

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

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

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

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

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

  9. 【leetcode】Binary Tree Postorder Traversal (hard) ☆

    二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> an ...

随机推荐

  1. Android自定义View自定义属性

    1.引言 对于自定义属性,大家肯定都不陌生,遵循以下几步,就可以实现: 自定义一个CustomView(extends View )类 编写values/attrs.xml,在其中编写styleabl ...

  2. SerialChat与Arduino的配合使用

    最近在开发过程中,用到了Arduino开发板以及其IDE:Arduino,这个IDE使用起来很方便,编码也很简单,但有一点美中不足的是Arduino只能输出数值,不能绘图,所以就用到了另外一款串口调试 ...

  3. SQL2008游标

    最近让写一个自动生成数据的存储过程,其中会遍历表中数据并做出相应处理,因为数据量不算太大所以使用到了游标,初识游标遇到几个小问题,所以来和大家一起分享一下: 使用游标的五个步骤: 1.声明游标 语法: ...

  4. 使用R进行倾向得分匹配

    pacman::p_load(knitr, wakefield, MatchIt, tableone, captioner)set.seed(1234)library(wakefield)df.pat ...

  5. 将InfoObject作为信息提供者Characteristic is InfoProvider

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 反编译CHM文件

    1.进入dos 2.输入 HH.EXE -decompile <输出路径> <目标chm文件> 例如:hh.exe -decompile d:\heihei D:\123.ch ...

  7. Lucas定理

    Lucas' theorem In number theory, Lucas's theorem expresses the remainder of division of the binomial ...

  8. 从ASP了解Http Buffer

    he Buffer property specifies whether to buffer the output or not. When the output is buffered, the s ...

  9. chain.doFilter(req, resp)

    web中的Filiter过滤器: 当req不改变时,filiter在web中的配置和顺序没有关系: 但当在filiter中将其改变类型时,会导致其改变的request类型包装层次过多,无法获取其中的参 ...

  10. Excle隐藏及展开列

    当excle文档类目比较多的时候我们希望看第一列和某一列的对应关系可以选择隐藏中间列. 选中要隐藏的列,然后右键-->隐藏即可 需要展开的时候,选中:被隐藏列的前一列和后一列,然后当鼠标在列头( ...