0

The problem is asking for an output of all paths in a binary tree in an arraylist of strings, with format a->b, if b is a child of a.

I am getting the following error messages, but I think I have "paths", "dfs" spelled and used correctly:

/code/Solution.java:41: error: cannot find symbol
paths.add(updated);
^
symbol: variable updated
location: class Solution
/code/Solution.java:46: error: cannot find symbol
dfs(paths, updated, toSearch.left);
^
symbol: variable updated
location: class Solution
/code/Solution.java:50: error: cannot find symbol
dfs(paths, updated, toSearch.right);
^
symbol: variable updated
location: class Solution
3 errors

Here is my code; I have commented my code for readability:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root of the binary tree
     * @return: all root-to-leaf paths
     */
    public List<String> binaryTreePaths(TreeNode root) {
        // paths would be the arraylist holding all paths for returning
        List<String> paths = new ArrayList<String>();
        
        // dummy string to get the code started
        String dummy = "";

        // the first call to dfs
        dfs(paths, dummy, root);
        return paths;
    }

    /** This function adds to the arraylist "path" if the TreeNode toSearch is a leaf.
    *   Otherwise it recursively calls itself on the child nodes until reaching a leaf
    **/
    private void dfs(List<String> paths, String current, TreeNode toSearch){
        // base case
        if(toSearch == null){
            return;
        }
        // this is to take care of the initial call when the String is empty - we do not want the string to start with "->".
        if(current == ""){
            String updated = Integer.toString(toSearch.val);
        }else{
            String updated = current + "->" + toSearch.val;
        }
        
        // if both children are null, then we can append the path to the list
        if(toSearch.left == null && toSearch.right == null){
            paths.add(updated);
            return;
        }
        // if either node is not null, we keep searching.
        if(toSearch.left != null){
            dfs(paths, updated, toSearch.left);
        }

        if(toSearch.right != null){
            dfs(paths, updated, toSearch.right);
        }




    }


}
kd8sp
  • 11
  • 2

2 Answers2

0

The way that variable scope works in Java is that if you define your variable within an if statement, it will only be usable, or in scope, within the if statement. In addition the if statement and the else statement have different scopes. So in each of those scopes there exists a variable called updated that is a String. In order to get the behavior that you want, you should define the variable outside the if statement and then use it.

String updated = current;
if (updated.equals("")) {
    updated += toSearch.val;
} else {
    updated += "->" + toSearch.val;
}

Also, you should use .equals() not == when comparing equality of strings.

0

Try this.

record TreeNode(int val, TreeNode left, TreeNode right) {}

public static List<String> binaryTreePaths(TreeNode root) {
    List<String> paths = new ArrayList<>();
    dfs(paths, "", root);
    return paths;
}

static void dfs(List<String> paths, String current, TreeNode toSearch) {
    if (toSearch == null)
        return;
    current += toSearch.val;
    if (toSearch.left == null && toSearch.right == null) {
        paths.add(current);
        return;
    }
    current += "->";
    dfs(paths, current, toSearch.left);
    dfs(paths, current, toSearch.right);
}

and

public static void main(String[] args) {
    TreeNode root =
        new TreeNode(3,
            new TreeNode(1, null, null),
            new TreeNode(6,
                new TreeNode(5,
                    new TreeNode(4, null, null),
                    null),
                new TreeNode(7, null, null)));
    List<String> paths = binaryTreePaths(root);
    System.out.println(paths);
}

output:

[3->1, 3->6->5->4, 3->6->7]
英語は苦手
  • 793
  • 1
  • 8