这是一道将二叉树先序遍历,题目不难。

首先采用深搜递归

/**
* 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的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 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日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  5. 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 ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  10. LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)

    144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...

随机推荐

  1. python3 + selenium + eclipse 中报:Unable to find a matching set of capabilities

    在环境python3 + selenium + eclipse 运行报错::Unable to find a matching set of capabilities 解决办法:Update Fire ...

  2. SpringBoot之导入导出Excel

    1.添加springBoot支持 <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...

  3. 从0起步 BI 第一课

    收集 BI 学习资料(了解,书籍,示例demo) 专业人士: http://www.cnblogs.com/aspnetx/p/3189029.html http://www.flybi.net/ h ...

  4. Java虚拟机学习 - 体系结构 内存模型(转载)

    一:Java技术体系模块图 二:JVM内存区域模型 1.方法区 也称"永久代” .“非堆”,  它用于存储虚拟机加载的类信息.常量.静态变量.是各个线程共享的内存区域.默认最小值为16MB, ...

  5. [pe531]Chinese leftovers

    题意:1e6~1e6+5000之间任意两个之间同余方程组的解.余数为欧拉函数. 解题关键:线性筛预处理,扩展中国剩余定理暴力求解. #include<cstdio> #include< ...

  6. IIS PHP的Loaded Configuration File为空解决[转]

    在Windows Server 2003上,IIS配置支持PHP,发现PHP扩展未加载,phpinfo()查看,显示 Configuration File (php.ini) Path (none) ...

  7. FASTQ格式

    FASQT格式是用于存储生物序列(通常是核苷酸序列)及其相应的碱基质量分数的一种文本格式.为简洁起见,序列字母和质量分数均使用单个ASCII字符进行编码.最初由Wellcome Trust Sange ...

  8. day4 函数重载

    函数的重载 1.函数重载的定义:在同一个类中,有一个以上的同名函数,只要函数的参数列表或参数类型不一样即可,与返回值无关, 这些统称为方法的重载. 2.函数的重载存在的原因:为了增强方法的阅读性,优化 ...

  9. SpringMVC使用fastjson自定义Converter支持返回jsonp格式(转)

    import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import c ...

  10. JavaScript 数据访问(通译自High Performance Javascript 第二章) [转]

    JavaScript 数据访问(通译自High Performance Javascript 第二章)   JavaScript 数据访问(翻译自High Performance Javascript ...