码迷,mamicode.com
首页 > 其他好文 > 详细

Problem Binary Tree Preorder Traversal

时间:2014-07-07 16:34:34      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   io   

Problem  Description:

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

 Solution:

 1     public List<Integer> preorderTraversal(TreeNode root) {
 2         if (root == null) return new LinkedList<Integer>();
 3         List<Integer> list = new LinkedList<Integer>();
 4         if (root.left == null && root.right == null) {
 5             List<Integer> leaf = new LinkedList<Integer>();
 6             leaf.add(root.val);
 7             return leaf;
 8         } 
 9         list.add(root.val);
10         if (root.left != null) list.addAll(preorderTraversal(root.left));
11         if (root.right != null) list.addAll(preorderTraversal(root.right));
12         
13         return list;
14     }

 

Problem Binary Tree Preorder Traversal,布布扣,bubuko.com

Problem Binary Tree Preorder Traversal

标签:des   style   blog   color   strong   io   

原文地址:http://www.cnblogs.com/liew/p/3815019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!