Java recursive solution

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