[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal
【题目】
Given a binary tree, return the preordertraversal of its nodes' values.
Example:
Input:[1,null,2,3]
1
\
2
/
3 Output:[1,2,3]
【思路】
有参考,好机智,使用堆栈压入右子树,暂时存储。
左子树遍历完成后遍历右子树。
【代码】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
LinkedList<Integer> ans=new LinkedList<Integer>();
Stack<TreeNode> tmp=new Stack<TreeNode>();
while(root!=null){
ans.add(root.val);
if(root.right!=null){
tmp.push(root.right);
}
root=root.left;
if(root==null&&!tmp.isEmpty()){
root=tmp.pop();
}
}
return ans;
}
}
[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal的更多相关文章
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- [Swift]LeetCode144. 二叉树的前序遍历 | Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- LeetCode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)
589. N叉树的前序遍历 589. N-ary Tree Preorder Traversal LeetCode589. N-ary Tree Preorder Traversal 题目描述 给定一 ...
- 【Leetcode】【Medium】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
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- 前端JS常见面试题(代码自撸)
题目一示例: 适用于子数组等长度及不等长度. let arr = [ [1,2,3], [5,6,7,8], [9,10,11,12,13] ] function arrayDiagonal(arr) ...
- 微信内置安卓x5浏览器请求超时自动重发问题处理小记
X5内核 请求超时后会自动阻止请求返回并由代理服务器将原参数重新发送请求到服务层代码.但由于第一次请求已经请求到服务器,会导致出现重复下单.支付等重大问题. 该问题由于腾讯x5浏览器会自动阻止第一次 ...
- vim格式化markdown表格
title: vim格式化markdown表格 date: 2017-11-23 15:23:25 tags: vim categories: 开发工具 安装插件 https://github.com ...
- Tomcat增加Context配置不带项目名访问导致启动的时候项目加载两次
eclipse发布web应用至tomcat,默认方式下访问该项目是需要带项目名称的,例http://localhost:8080/myapp/.现在需要改成这样访问http://localhost.修 ...
- MongoDB 之 你得知道MongoDB是个什么鬼 MongoDB - 1
最近有太多的同学向我提起MongoDB,想要学习MongoDB,还不知道MongoDB到底是什么鬼,或者说,知道是数据库,知道是文件型数据库,但是不知道怎么来用 那么好,所谓千呼万唤始出来,现在我就拉 ...
- mybatis常见问题和错误
1. jdbc java type 映射关系 1) mysql的text 在mybatis中使用varchar类型 2. mybatis常见的错误 3.There is no getter for p ...
- <转>jmeter(二十一)jmeter常用插件介绍
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- Signal in unit is connected to following multiple drivers VHDL
参考链接 https://blog.csdn.net/jbb0523/article/details/6946899 出错原因 两个Process都对LDS_temp进行了赋值,万一在某个时刻,在两个 ...
- Apache web服务器(LAMP架构)
Apache web服务器(LAMP架构) apache介绍 1).世界上使用率最高的网站服务器,最高时可达70%:官方网站:apache.org 2).http 超文本协议 HTML 超文本标记语言 ...
- C# 关闭子窗体释放子窗体对象问题
1 在主窗口中实例化子窗口 Form2 f2 = new Form2(); 2 通过按钮来显示子窗口 f2.Show(); 3 关闭子窗口而不释放子窗口对象的方法 protected override ...