原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/

题意:这题用递归比较简单。应该考察的是使用非递归实现二叉树的先序遍历。先序遍历的遍历顺序是:根,左子树,右子树。

解题思路:如果树为下图:

                      1

                     /     \

                    2         3

                   /     \    /    \

                   4       5  6     7

    使用一个栈。步骤为:

    一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}。

    二,开始弹栈,当栈顶元素为2时,弹出2,并检测2存在右孩子5,于是遍历5并入栈,此时栈为{1,5}。

    三,弹出5,5没有左右孩子,继续弹栈,将1弹出后,栈为{}。

    四,由于1存在右孩子,则继续按照以上步骤入栈出栈。{3, 6}->{7}->{},结束。

    栈的状态变化:{1}->{1,2}->{1,2,4}->{1,2}->{1}->{1,5}->{1}->{}->{3}->{3,6}->{3}->{}->{7}->{}。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a list of integers
def iterative_preorder(self, root, list):
stack = []
while root or stack:
if root:
list.append(root.val)
stack.append(root)
root = root.left
else:
root = stack.pop()
root = root.right
return list def recursive_preorder(self, root, list):
if root:
list.append(root.val)
self.preorder(root.left,list)
self.preorder(root.right,list) def preorderTraversal(self,root):
list = []
self.iterative_preorder(root,list)
return list

[leetcode]Binary Tree Preorder Traversal @ Python的更多相关文章

  1. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

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

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

  3. [LeetCode] Binary Tree Preorder Traversal

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

  4. LeetCode——Binary Tree Preorder Traversal

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

  5. LeetCode Binary Tree Preorder Traversal 先根遍历

    题意:给一棵树,求其先根遍历的结果. 思路: (1)深搜法: /** * Definition for a binary tree node. * struct TreeNode { * int va ...

  6. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  7. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

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

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

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

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

随机推荐

  1. freertos的钩子函数

    在main中添加: /** * @brief FreeRTOS 内存分配失败钩子函数 */ void vApplicationMallocFailedHook(void) { taskDISABLE_ ...

  2. 深度学习基础系列(十)| Global Average Pooling是否可以替代全连接层?

    Global Average Pooling(简称GAP,全局池化层)技术最早提出是在这篇论文(第3.2节)中,被认为是可以替代全连接层的一种新技术.在keras发布的经典模型中,可以看到不少模型甚至 ...

  3. [leetcode DP]91. Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  4. web服务端安全之SQL注入攻击

    一.SQL注入攻击的原理攻击者在HTTP请求中,注入恶意的SQL代码,并在服务端执行.比如用户登录,输入用户名camille,密码 ' or '1'='1 ,如果此时使用参数构造的方式,就会出现 ' ...

  5. scrapy爬虫,爬取图片

    一.scrapy的安装: 本文基于Anacoda3, Anacoda2和3如何同时安装? 将Anacoda3安装在C:\ProgramData\Anaconda2\envs文件夹中即可. 如何用con ...

  6. 在Eclipse中修改web项目的名称

    在Eclipse中修改web项目的名称 一.误区: 单击要修改名称的项目上右键Refactor->Rename,然后修改成另外一个名称 (光这样是不够的,哪怕你再修改web.xml中的displ ...

  7. Cipolla算法学习小记

    转自:http://blog.csdn.net/doyouseeman/article/details/52033204 简介 Cipolla算法是解决二次剩余强有力的工具,一个脑洞大开的算法. 认真 ...

  8. [Agc002E]Candy Piles

    [Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...

  9. HTTP首部字段

    HTTP首部由首部字段名和首部字段值组成,以逗号隔开.如果首部出现重复,有些浏览器优先处理第一个出现的首部,有些优先处理后者. 主要分为四大类 通用首部字段 请求首部字段 响应首部字段 实体首部字段 ...

  10. 【Codeforces 949D】Shake It! 【动态规划】

    参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...