97% Java solution


  • 0
    L
    public List<String> binaryTreePaths(TreeNode root) {
            List<String> list = new ArrayList();
            helper(root,list,"");
            return list;
        }
        
        private void helper(TreeNode root, List<String> list, String s) {
            if(root==null) {
                return;
            }
            if(root.left==null && root.right==null) {
                s = s + root.val;
                list.add(s);
                return;
            }
            helper(root.left,list,s+root.val+"->");
            helper(root.right,list,s+root.val+"->");
        }
    

Log in to reply
 

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