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) ...
随机推荐
- android性能优化练习:过度绘制
练习:https://github.com/zhangbz/AndroidUIPorblems 查看过度绘制 在开发者选项中开启"调试GPU过度绘制" 判断标准 无色:没有过度绘制 ...
- Magical平台类库代码分享
这些天闲来无事,就整理了一些类库.jQuery插件和自定义控件.今天和大家分享下Magical平台类库代码. 下图为整个解决方案图.MagicalPlatForm里面定义的是众多的Layer层:Mag ...
- 在数组中找出x+y+z=0的组合
就是找x+y=-z的组合 转化为找出值为-z满足x+y=-z的组合 解法一: 为了查找,首先想到排序,为了后面的二分,nlogn, 然后x+y的组合得n^2的复杂度,加上查找是否为-z,复杂度为nlo ...
- Linux make: g++: Command not found
Linux使用make命令时遇到"make: g++: Command not found",这个主要是没有安装gcc-c++.x86_64,如下所示 [root@localh ...
- YourSQLDba版本升级总结
在使用YourSQLDba做数据库备份.维护时,像其它软件一样,版本升级是不可避免的.因为YourSQLDba一直在不停更新版本.扩展功能.下面介绍一下升级YourSQLDba时的具体步骤和一些注意事 ...
- Linux strace命令
简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...
- MongoDB学习笔记~自己封装的Curd操作(按需更新的先决条件)
回到目录 我们上一讲中介绍了大叔封装的Mongo仓储,其中介绍了几个不错的curd操作,而对于按需更新内部子对象,它是有条件的,即你的子对象不能为null,也就是说,我们在建立主对象时,应该为子对象赋 ...
- chrome 浏览器的预提取资源机制导致的一个请求发送两次的问题以及ClientAbortException异常
调查一个 pdf 打印报错: ExceptionConverter: org.apache.catalina.connector.ClientAbortException: java.net.Sock ...
- 【Linux学习】Linux下用户组、文件权限详解
原文地址:http://www.cnblogs.com/123-/p/4189072.html Linux下用户组.文件权限详解 用户组 在linux中的每个用户必须属于一个组,不能独立于组外.在li ...
- log4j配置详解
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...