My accepted java solution


  • 0
    D
    public class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> list = new ArrayList<String>();
            return binaryTreePaths(root,list, "");
        }
        public List<String> binaryTreePaths(TreeNode root,List<String> list,String s) {
            if(root == null) {
                return list;
            }
            s =  s + "->" + Integer.toString(root.val);
            if(root.left == null && root.right == null) {
                s = s.substring(2); 
                list.add(s);
            }
            binaryTreePaths(root.left,list,s);
            binaryTreePaths(root.right,list,s);
            return list;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.