my simple Java solution


  • 0
    J
    public class Solution {
        List<String> result = new ArrayList<String>();
        public List<String> binaryTreePaths(TreeNode root) {
            String s;
            if(root == null) return result;
            s = root.val+"";
            if(root.left != null) pathHelp(root.left,s);
            if(root.right != null) pathHelp(root.right,s);
            if(root.left == null && root.right == null) result.add(s);
            return result;
        }
        
        public void pathHelp(TreeNode node, String s){
            s = s+ "->"+node.val;
            if(node.left == null && node.right == null) result.add(s);
            if(node.left != null) pathHelp(node.left,s);
            if(node.right != null) pathHelp(node.right,s);
        }
    }

Log in to reply
 

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