Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,2,3].

代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list=new ArrayList<>();
if(root==null)
return list; list.add(root.val); if(root.left!=null)
list.addAll(preorderTraversal(root.left));
if(root.right!=null)
list.addAll(preorderTraversal(root.right)); return list;
}
}

144. Binary Tree Preorder Traversal的更多相关文章

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

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

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

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

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

  4. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

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

  6. leetcode 144. Binary Tree Preorder Traversal ----- java

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. Java [Leetcode 144]Binary Tree Preorder Traversal

    题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...

  8. 【LeetCode】144. Binary Tree Preorder Traversal

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

  9. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

随机推荐

  1. adMob iAd整合,随机根据网络状况自动显示。

    最近找整合的代码,找到的都不对,有个大概对的,但要奔溃退出,只要两个单独弄. adMob 下载好sdk,导入进去,iAd的加入iad framework. 使用方法,在viewController v ...

  2. linux修改系统时间和linux查看时区、修改时区的方法

    一.查看和修改Linux的时区 1. 查看当前时区命令 : "date -R" 2. 修改设置Linux服务器时区方法 A命令 : "tzselect" 方法 ...

  3. $where $options: 'g','i'

    db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...

  4. CCNA 6.5

    no sh (no shutdown : start the interface)   router rspf 1 network x.x.x.x x.x.x.x area 0 int (interf ...

  5. JSON基础使用

    1)JSON概念 JSON 是纯文本 JSON 具有“自我描述性”(人类可读) JSON 具有层级结构(值中存在值) JSON 可通过 JavaScript 进行解析 JSON 数据可使用 AJAX ...

  6. Javascript使用总结

    Javascript(简称JS)简介 JavaScript是一门广泛用于浏览器客户端的脚本语言,由Netspace公司设计,当时跟Sun公司(已经被oracle收购)合作,所以名字起得像Java,业内 ...

  7. iphone/ipod网页开发教程及规则

    侦测iPhone/iPod 开发特定设备的移动网站,首先要做的就是设备侦测了.下面是使用Javascript侦测iPhone/iPod的UA,然后转向到专属的URL. if((navigator.us ...

  8. JAVA 打印九九乘法表

    /** *  * @author liangxiaoyu * @version 1.0 *2015-09-18 */public class JJ { public static void main( ...

  9. 关于struts2拦截器获取页面参数

    package InterCeptor; import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;impor ...

  10. System.out.println()输出到指定文件里

    public static void main(String[] args) throws Exception{ String str = "abcd"; PrintStream ...