Leet-code144. Binary Tree Preorder Traversal
这是一道将二叉树先序遍历,题目不难。
首先采用深搜递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public static List<Integer> resultlist = new ArrayList<Integer>(); public static void dfs (TreeNode root ,boolean flag ) {
if(flag==true)
{
resultlist.clear();
}
if(root==null)
{
return ;
} resultlist.add(root.val); if(root.left!=null)
{
dfs(root.left,false);
}
if(root.right!=null)
{
dfs(root.right,false);
} } public static List<Integer> preorderTraversal(TreeNode root ) {
dfs(root,true);
return resultlist;
} }
非递归实现方式
public static List<Integer> preorderTraversal(TreeNode root ) {
List<Integer> result = new ArrayList<>(); if(root == null)
return result; Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root); while(!stack.empty()){
TreeNode n = stack.pop();
result.add(n.val); if(n.right != null){
stack.push(n.right);
}
if(n.left != null){
stack.push(n.left);
} }
return result;
}
Leet-code144. Binary Tree Preorder Traversal的更多相关文章
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 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 (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 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 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
随机推荐
- Java的Socket通信(多线程)(1)
如图: 思路: ①首先创建服务器端Socket,指定并侦听某一个端口,然后循环监听开始等待客户端的连接…. ②创建客户端socket,指定服务器地址和端口,然后获取输出流,向服务器端发送请求,并关闭s ...
- Oracle的rowid
ROWID是数据的详细地址,通过rowid,oracle可以快速的定位某行具体的数据的位置. ROWID可以分为物理rowid和逻辑rowid两种.普通的堆表中的rowid是物理rowid,索引组织表 ...
- 关于WPF的弹出窗口
几个重要的概念需要清楚: Show和ShowDialog区别 1.调用Show方法后弹出子窗口后,线程会继续往下执行.调用ShowDialog方法弹出子窗口后,线程会阻塞,直到子窗口关闭才继续往下执行 ...
- 锐捷认证的一些问题&解决方法
scau锐捷校园网各种无法吐槽,认证有时候自己掉线了麻痹都打到boss了给我掉线,收费也坑爹,连铁通都比不上. 1.锐捷认证客户端已停止工作: 貌似是毒霸的问题,把金山毒霸关掉再试 2.获取ip地址信 ...
- 1、CDH集群搭建
一.准备工作 1.系统环境 系统centos6.5 节点三台: 192.168.1.130 192.168.1.131 192.168.1.132 1.所有节点关闭防火墙 service iptabl ...
- JSP 标准标签库(JSTL)(菜鸟教程)
菜鸟教程 JSTL 1.1 与 JSTL 1.2 之间的区别?如何下载 JSTL 1.2? JSTL 1.2 中不要求 standard.jar 包. 您可以在 Maven 中央仓库中找到它们. ht ...
- PHP SOAP 提交XML
<?php $xmldata = <<<EOT <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap. ...
- 洛谷P3797 妖梦斩木棒
P3797 妖梦斩木棒 题目背景 妖梦是住在白玉楼的半人半灵,拥有使用剑术程度的能力. 题目描述 有一天,妖梦正在练习剑术.地面上摆放了一支非常长的木棒,妖梦把它们切成了等长的n段.现在这个木棒可以看 ...
- codevs3002 石子归并3
3002 石子归并 3 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安 ...
- PJzhang:一站式跨境卖家网址导航Amz520.com
猫宁!!! 参考链接:http://www.guxiaobei.com/amz520-release.html www.amz520.com是一个跨境电商导航站点,汇集了大量的高效信息,做这个站点花费 ...