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. 1
  2. \
  3. 2
  4. /
  5. 3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

  1. '''
  2. Created on Nov 18, 2014
  3.  
  4. @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
  5. '''
  6. # Definition for a binary tree node
  7. # class TreeNode:
  8. # def __init__(self, x):
  9. # self.val = x
  10. # self.left = None
  11. # self.right = None
  12.  
  13. class Solution:
  14. # @param root, a tree node
  15. # @return a list of integers
  16. def preorderTraversal(self, root):
  17. stack=[]
  18. vals=[]
  19. if(root==None): return vals
  20. node=root
  21. stack.append(node)
  22. while(len(stack)!=0):
  23. node=stack.pop()
  24. if(node==None): continue
  25. vals.append(node.val)
  26. stack.append(node.right)
  27. stack.append(node.left)
  28.  
  29. return vals

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

  1. 1
  2. \
  3. 2
  4. /
  5. 3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

  1. '''
  2. Created on Nov 18, 2014
  3.  
  4. @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
  5. '''
  6. # Definition for a binary tree node
  7. # class TreeNode:
  8. # def __init__(self, x):
  9. # self.val = x
  10. # self.left = None
  11. # self.right = None
  12.  
  13. class Solution:
  14. # @param root, a tree node
  15. # @return a list of integers
  16. def inorderTraversal(self, root):
  17. stack=[]
  18. vals=[]
  19. visited={}
  20. if(root==None): return vals
  21. node=root
  22. stack.append(node)
  23. visited[node]=1
  24. while(len(stack)!=0):
  25. if(node.left!=None and visited.has_key(node.left)==False):
  26. node=node.left
  27. stack.append(node)
  28. visited[node]=1
  29. else:
  30. node=stack.pop()
  31. if(node==None): continue
  32. vals.append(node.val)
  33. if(node.right!=None):
  34. stack.append(node.right)
  35. node=node.right
  36. return vals

Binary Tree Postorder Traversal

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

For example:
Given binary tree {1,#,2,3},

  1. 1
  2. \
  3. 2
  4. /
  5. 3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

  1. '''
  2. Created on Nov 19, 2014
  3.  
  4. @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
  5. '''
  6. # Definition for a binary tree node
  7. # class TreeNode:
  8. # def __init__(self, x):
  9. # self.val = x
  10. # self.left = None
  11. # self.right = None
  12.  
  13. class Solution:
  14. # @param root, a tree node
  15. # @return a list of integers
  16. def postorderTraversal(self, root):
  17. visited={}
  18. stack=[]
  19. vals=[]
  20. if(root==None): return vals
  21. node=root
  22.  
  23. stack.append(node)
  24. visited[node]=1
  25.  
  26. while(len(stack)!=0):
  27. node=stack[-1]
  28. if(node.left !=None and visited.has_key(node.left)==False):
  29. stack.append(node.left)
  30. visited[node.left]=1
  31. continue
  32. else:
  33. if(node.right!=None and visited.has_key(node.right)==False):
  34. stack.append(node.right)
  35. visited[node.right]=1
  36. continue
  37. node=stack.pop()
  38. if(node==None): continue
  39. vals.append(node.val)
  40.  
  41. return vals

LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历的更多相关文章

  1. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. Binary Tree的3种非Recursive遍历

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

  3. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  4. 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历

    题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...

  5. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  6. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  7. LeetCode Binary Tree Paths(简单题)

    题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...

  8. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  9. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

随机推荐

  1. Fiddler-3 配置Fiddler监听iphone的http/https请求

    电脑端可以通过Fiddler监听手机端的http请求.需要两个步骤:首先配置Fiddler,再配置手机端. 1 配置 Fiddler 允许远程设备连接: 菜单Tools - Telerik Fiddl ...

  2. #define用法解析

    #define Add(a,b) a+b; 在一般使用的时候是没有问题的,但是如果遇到如: c * Add(a,b) * d 的时候就会出现问题,代数式的本意是a+b然后去和c,d相乘,但是因为使用了 ...

  3. HTML之iframe

    iframe:是框架的一种形式. 属性: frameborder=0/1 表示是否显示周围边框 0--否 1--是 width,height:设置的边框宽高,具体数值不需要加单位,也可用百分比 mar ...

  4. PKU 1001解题代码

    本来以前也写过,但是由于许多细节问题,没有AC,今天修改了一下,终于AC了,以前没有AC的具体原因总结了了一下,必须任何数的0次方都等于1没有考虑,还有就是首0和末尾0以及小数点没有处理好,下面贴代码 ...

  5. 关于eclipse中DDMS中Emulator Control选项卡为灰色不可用

    首先先感谢版主:http://blog.csdn.net/noname666/article/details/51670905#reply 方法一的出处:http://stackoverflow.co ...

  6. 删除hao123这个恶心的毒瘤

    最近做服务器,好好一个东西莫名其妙的被染上了这个狗皮膏药......然后我就用了各种手段删除,注册表.组策略等等都用上了,却没有丝毫办法.....最后发现的地方特别无语,居然在快捷方式的属性中加上了u ...

  7. SIP模块版本错误问题:the sip module implements API v??? but XXX module requires API v???

    系统安装了python 2.7,继续安装PyQt4,于是依次下载sip.pyqt4源码进行安装.用以下代码测试: import PyQt4.QtGui 显示出错.错误信息:the sip module ...

  8. JXL操作Excel

    jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一点.但jExcelAPI对中文 ...

  9. SqlServer性能优化 即席查询(十三)

    执行计划,查询类别: 1.即席查询     2.预定义查询 select c.EnglishProductCategoryName,p.EnglishProductName,p.Color,p.Siz ...

  10. update the UI property cross thread

    this.Invoke((MethodInvoker)delegate { txtResult.Text = sbd.ToString(); // runs on UI thread });