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"]
/**
* 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<String> binaryTreePaths(TreeNode root) {
List<String> result = new LinkedList<String>();
if(root == null) {
return result;
}
findPaths(result,"",root);
return result;
}
public void findPaths(List<String> result,String temp,TreeNode root) {
if(root == null) {
return;
}
if(root.left == null && root.right == null) {
result.add(temp+Integer.toString(root.val));
return;
}
findPaths(result,temp+Integer.toString(root.val)+"->",root.left);
findPaths(result,temp+Integer.toString(root.val)+"->",root.right);
}
}