Leetcode 144. Binary Tree Preorder Traversal

参考例子:[8,3,1,6,4,7,10,14,13]
8,3,1 和 6,4 说明从root开始,沿着左臂向下寻找leaf 的过程中应该逐个将node.val push入ans.
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
ans = []
while stack or root:
while root:
stack.append(root)
ans.append(root.val)
root = root.left
root = stack.pop()
root = root.right
return ans
Leetcode 144. Binary Tree Preorder Traversal的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- LeetCode 144. Binary Tree Preorder Traversal 动态演示
先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) ...
随机推荐
- 如何设置TextView控件的背景透明度和字体透明度
如何设置TextView控件的背景透明度和字体透明度 设计师给的标注都是类似这样的: 字号:26 颜色:#000000 透明度:80% 其实,程序上只要需要一个色值就OK了,那么这个色值我如何计算呢? ...
- AppDelegate动态加载StoryBoard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...
- Android Studio连接真机没反应?
刚好遇到这个问题,在网上百度了一下,看到有人分享了引起该问题的几个原因: 手机设置问题.开USB调试 方法: 手机设置 - 开发人员选项 - USB调试 - 勾选 数据线问题. 有的数据线只能用来充电 ...
- ASP.NET获取真正的客户端IP地址的6种方法
Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真 ...
- python头部注释 vim添加头部注释
1.先说说python和virtual python 一般环境下,python解释器会放在/usr/bin/ 下面,然后你执行python的时候就会运行了,但是如果没有在/usr/bin/下面的话,执 ...
- SQLite使用(一)&&选择表类型
在SQLite中,主要有两种表类型,带rowid的表和不带rowid的表.我们利用create table 建一张表,默认都会有一个隐含名字为rowid的主键,暂且称带rowid的表为普通表.如果建表 ...
- oracle 使用 dbms_lock.sleep暂停存储过程执行
grant execute on dbms_lock to USERNAME; dbms_lock.sleep(time)参数单位为秒 create or replace procedure ...
- wubi安装ubuntukylin-14.04
自ubuntukylin-14.04发布以来一直想体验一下,正好五一休假有了时间,在经历了一番坎坷后终于安装成功.安装环境为Win7家庭高级版,至于采用wubi方式安装,只因Linux水平差和图省事, ...
- 【转帖】分享一个迅为4412开发板OTG烧录批处理文件
平台:iTOP-4412开发板 Bat 功能: 1.可以分条的执行烧录,不需要每次烧录都去复制命令 2.可以批量烧录 开发板系统烧录批处理文件,请将此文件放置在fastboot程序同目录下,下载地址: ...
- C# 将绝对路径转换为相对路径
引言 在项目中常需要将绝对路径,转换为相对路径,来增加程序相关配置的的灵活性(不用因为整体挪个位置就导致我们的程序不能正常工作) 解决问题方法 自己写代码解决: private strin ...