Java recursive solution


  • 2
    D
    public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        if(root==null) return res;
        String tmp = ""+root.val;
        helper(root,res,tmp);
        return res;
    }
    private void helper(TreeNode root, List<String> res, String tmp) {
        if(root.left==null&&root.right==null) {
            res.add(tmp);
        } else {
            if(root.left!=null) helper(root.left, res, tmp+"->" + root.left.val);
            if(root.right!=null) helper(root.right, res, tmp+"->" + root.right.val);
        }
    }
    

    }


Log in to reply
 

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