原题地址: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. Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

    最近在用python做数据挖掘,在聚类的时候遇到了一个非常恶心的问题.话不多说,直接上代码: from sklearn.cluster import KMeans from sklearn.decom ...

  2. 2017-2018-1 20179202《Linux内核原理与分析》第八周作业

    一 .可执行程序的装载 1. 预处理.编译.链接 gcc –e –o hello.cpp hello.c //预处理 gcc -x cpp-output -S -o hello.s hello.cpp ...

  3. dSploitzANTI渗透教程之安装zANTI工具

    dSploitzANTI渗透教程之安装zANTI工具 Dsploit/zANTI基础知识 zANTI是一款Android平台下的渗透测试工具,支持嗅探已连接的网络.支持中间人攻击测试.端口扫描.Coo ...

  4. 1008 Elevator (20)(20 point(s))

    problem The highest building in our city has only one elevator. A request list is made up with N pos ...

  5. 错误:SSL peer shut down incorrectly

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 打开这个界面 ,修改一下.对照自己以前的没问题的项目.我的是3.3. 错误:Failed ...

  6. 【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)

    Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every ski ...

  7. pear中几个实用的xml代码库

    1.XML_Beautifier 用于将一段排版凌乱的XML文档美化 <?php require_once "XML/Beautifier.php"; $fmt = new ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题

    A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...

  9. Codeforces Beta Round #97 (Div. 1) C. Zero-One 数学

    C. Zero-One 题目连接: http://codeforces.com/contest/135/problem/C Description Little Petya very much lik ...

  10. ASP.NET Web API 记录请求响应数据到日志的一个方法

    原文:http://blog.bossma.cn/dotnet/asp-net-web-api-log-request-response/ ASP.NET Web API 记录请求响应数据到日志的一个 ...