Problem Link:

http://oj.leetcode.com/problems/binary-tree-preorder-traversal/

Even iterative solution is easy, just use a stack storing the nodes not visited. Each iteration, pop a node and visited it, then push its right child and then left child into stack if possible.

Note the special case that the root is NULL.

The python code is as follows, which is accepted by OJ.leetcode.

# 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 preorderTraversal(self, root):
"""
Iterative way using a stack, hope no LTE...
"""
res = []
non_visited = []
if root is not None:
non_visited.append(root)
while non_visited:
top = non_visited.pop()
res.append(top.val)
if top.right:
non_visited.append(top.right)
if top.left:
non_visited.append(top.left)
return res

  

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

  1. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  2. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  3. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  4. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  5. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  6. LeetCode OJ 144. Binary Tree Preorder Traversal

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

  7. LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)

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

  8. 【LeetCode】Binary Tree Preorder Traversal

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

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

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

随机推荐

  1. Linux编程

    头文件一般位于usr/include目录下,各个子类的头文件位于include子目录下 不知道某个“库函数”需要哪些头文件,使用“man 函数”即可查看 sys/types.h  基本系统数据类型 s ...

  2. Web前端开发面试题

    1. 以下的代码有问题吗?如果有你觉着应该如何修改? for(int i=0; i<list.size(); i++) {  .....  .....  if(...)  {   list.re ...

  3.  BP神经网络

     BP神经网络基本原理 BP神经网络是一种单向传播的多层前向网络,具有三层或多层以上的神经网络结构,其中包含输入层.隐含层和输出层的三层网络应用最为普遍. 网络中的上下层之间实现全连接,而每层神经元之 ...

  4. 平衡查找树之B树

    转自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 定义 B 树可以看作是对2-3查找树的一种扩展,即 ...

  5. GridControl的用法(1)

    一.属性设置 ①去除gridControl上的筛选条 //去除上面的筛选条            gridView1.OptionsView.ShowGroupPanel = false; ②设置列名 ...

  6. FZU 2216 The Longest Straight 模拟

    题目链接:The Longest Straight 就是一个模拟就是这样,T_T然而当时恶心的敲了好久,敲完就WA了,竟然有这么简单的方法,真是感动哭了.......xintengziji...zhi ...

  7. mysql linux备份shell

    #!/bin/bash# export and backup the activity.sql  mysqldump  -uname -password activity --skip-lock-ta ...

  8. K最近邻

    k算法实现的步骤: 第一:确定K值(就是指最近邻居的个数).一般是一个奇数,因为测试样本个数有限, 第二:确定度量的长度,也就是余弦值,根据公式来算:     然后根据这个距离,排序大小,从中选出前k ...

  9. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  10. [开发笔记]-DataGridView控件中自定义控件的使用

    最近工作之余在做一个百度歌曲搜索播放的小程序,需要显示歌曲列表的功能.在winform中采用DataGirdView来实现. 很久不写winform程序了,有些控件的用法也有些显得生疏了,特记录一下. ...