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

257. Binary Tree Paths

时间:2017-10-24 14:10:20      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:class   treenode   roo   root   ring   following   append   color   null   

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

 

   1
 /   2     3
   5

 

All root-to-leaf paths are:

["1->2->5", "1->3"]
题目含义:找到所有从根到叶子加点的路径
 1      public void appendChildren(TreeNode root,String curPath,List<String> answer) {
 2         if (root == null) return;
 3         if (root.left== null && root.right==null) answer.add(curPath+root.val);
 4         if (root.left!=null) appendChildren(root.left,curPath+root.val+"->",answer);
 5         if (root.right!=null) appendChildren(root.right,curPath+root.val+"->",answer);
 6     }   
 7     
 8     public List<String> binaryTreePaths(TreeNode root) {
 9          if(root == null) return new ArrayList<String>();
10         List<String> result = new ArrayList<>();
11         appendChildren(root,"",result);   
12         return result;
13     }

 

257. Binary Tree Paths

标签:class   treenode   roo   root   ring   following   append   color   null   

原文地址:http://www.cnblogs.com/wzj4858/p/7723059.html

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