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 class Solution { 2 public List<String> binaryTreePaths(TreeNode root) { 3 List<String> answer = new ArrayList<String>(); 4 if(root!=null) serBST(root,"",answer); 5 return answer; 6 } 7 private void serBST(TreeNode root,String path,List<String> answer){ 8 if(root.left==null && root.right==null) answer.add(path+root.val); 9 if(root.left!=null) serBST(root.left,path+root.val+"->",answer); 10 if(root.right!=null) serBST(root.right,path+root.val+"->",answer); 11 } 12 }