【LEETCODE OJ】Binary Tree Preorder Traversal
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的更多相关文章
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【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 ...
- 【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 ...
- LeetCode OJ 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
随机推荐
- Asp.net 解析json
Asp.net Json数据解析的一种思路 http://www.cnblogs.com/scy251147/p/3317366.html http://tools.wx6.org/json2csha ...
- 深入入门正则表达式(java)
一.入门基础 1.元字符 很多人对正则表达式的印象就是乱码..许许多多的符号组合在一起,偶见单词,正则确实是这样的,所以下面我们要看看这些符号都是什么意思 有些符号不是大家看到的字面上的意思:比如“. ...
- Android SDK的安装以及环境变量的配置
想来试试app的开发,于是便需要在电脑上安装各种软件包,其中Android SDK就是其中一种,特地记录一下其安装过程: 1.下载地址http://baoku.360.cn/soft/show/app ...
- JDE变量说明
BC Business view columns. Columns that are included in the attached business view. These columns are ...
- java之socket
socket系java中网络编程的内容. 1客户端: package com.wtd.socket; import java.io.BufferedReader; import java.io.IOE ...
- js 数组去除空值
for(var i = 0 ;i<wordarr.length;i++) { if(wordarr[i] == "& ...
- iOS 文件读写
#import <Foundation/Foundation.h> @interface Utils : NSObject +(void) writeFile:(NSString *) f ...
- UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- WDCP安装常用组件(memcache、mysqli、PDO_MYSQL、mysql innodb、libmcrypt、php zip)的方法
为有更好的性能,也为更简洁的系统,一些不是常用或不是基本的功能,都将做为可选的安装组件需要用到的就安装 注意:如果安装时遇到 ./configure: Permission denied提示,很有可能 ...
- tableview_nav 动画效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat yOffset = scrollView.contentOffset. ...