题目描述:
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"]
比较简单,直接上代码了,一次AC:
public class Solution {
List<String> result=new ArrayList<String>();
public List<String> binaryTreePaths(TreeNode root) {
if(root==null)
return result;
String str="";
getbinaryTreePaths(root, str);
return result;
}
public void getbinaryTreePaths(TreeNode root,String str){
if(root.left==null&&root.right==null){
str+=root.val;
result.add(str);
}
str+=root.val+"->";
if(root.left!=null)
getbinaryTreePaths(root.left, str);
if(root.right!=null)
getbinaryTreePaths(root.right, str);
}
}
423

被折叠的 条评论
为什么被折叠?



