LeetCode – Binary Tree Paths (Java)

Given a binary tree, return all root-to-leaf paths.

1. Naive DFS Solution

A typical depth-first search problem.

public List<String> binaryTreePaths(TreeNode root) {
    ArrayList<String> finalResult = new ArrayList<String>();
 
    if(root==null)
        return finalResult;
 
    ArrayList<String> curr = new ArrayList<String>();
    ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
 
    dfs(root, results, curr);
 
    for(ArrayList<String> al : results){
        StringBuilder sb = new StringBuilder();
        sb.append(al.get(0));
        for(int i=1; i<al.size();i++){
            sb.append("->"+al.get(i));
        }
 
        finalResult.add(sb.toString());
    }
 
    return finalResult;
}
 
public void dfs(TreeNode root, ArrayList<ArrayList<String>> list, ArrayList<String> curr){
    curr.add(String.valueOf(root.val));
 
    if(root.left==null && root.right==null){
        list.add(curr);
        return;
    }
 
    if(root.left!=null){
        ArrayList<String> temp = new ArrayList<String>(curr);
        dfs(root.left, list, temp);
    }
 
    if(root.right!=null){
        ArrayList<String> temp = new ArrayList<String>(curr);
        dfs(root.right, list, temp);
    } 
}

2. Simplified DFS Solution

This depth-first solution can be much simplified like the following:

public List<String> binaryTreePaths(TreeNode root) {
 
    String sb = "";
    ArrayList<String> result = new ArrayList<String>();
 
    helper(root, result, sb);
 
    return result;
}
 
public void helper(TreeNode root, ArrayList<String> result, String s){
    if(root==null){
        return;
    }
 
    s = s+"->"+root.val;
 
    if(root.left==null &&root.right==null){
        result.add(s.substring(2));
        return;
    }
 
    if(root.left!=null){
        helper(root.left, result, s);
    }
    if(root.right!=null){
        helper(root.right, result, s);
    }
}

3 thoughts on “LeetCode – Binary Tree Paths (Java)”

  1. Another Approach using hashMa trace path
    import java.util.HashMap;
    import java.util.Map;

    public class BinaryTreePaths {

    public static void main(String[] args) {
    Tree tree = new Tree();
    tree.root = new Tree.Node(1);
    tree.root.left = new Tree.Node(2);
    tree.root.right = new Tree.Node(3);

    tree.root.left.left = new Tree.Node(4);
    tree.root.left.right = new Tree.Node(5);

    BinaryTreePaths binaryTreePaths = new BinaryTreePaths();
    Map path = new HashMap();
    binaryTreePaths.findBinaryTreePaths(tree.root, path);
    }

    public void findBinaryTreePaths(Tree.Node root, Map path) {

    if(root.left == null && root.right == null){
    printPath(path, root.value);
    return;
    }

    if(root.left != null){
    path.put(root.left.value, root.value);
    findBinaryTreePaths(root.left, path);
    }

    if(root.right != null){
    path.put(root.right.value, root.value);
    findBinaryTreePaths(root.right, path);
    }
    }

    private void printPath(Map path, int value) {
    StringBuilder builder = new StringBuilder();

    builder.append(value);
    int key = value;
    while(path.containsKey(key)){
    builder.append(“>-“);
    builder.append(path.get(key));
    key = path.get(key);
    }
    //String str = builder.toString();
    System.out.println(builder.reverse());
    }

    }

  2. public class Solution {
    List al = new ArrayList();
    public List binaryTreePaths(TreeNode root) {

    if(root == null)
    {
    return al;
    }

    String s=””;
    calPath(root, s);
    return al;
    }

    public void calPath(TreeNode root, String s)
    {
    if(root.left==null && root.right==null)
    {
    al.add(s + root.val);
    return;
    }

    if(root.left!=null)
    {
    calPath(root.left, s + root.val + “->”);
    }

    if(root.right!=null)
    {
    calPath(root.right, s + root.val + “->”);
    }
    }
    }


  3. public class Solution
    {
    List result = new ArrayList();
    StringBuilder sb = new StringBuilder();

    public List binaryTreePaths(TreeNode root)
    {
    if(root == null)
    return result;

    int cntr = sb.length();
    if(root.left == null && root.right == null)
    {
    sb.append(root.val);
    result.add(sb.toString());
    sb.delete(cntr,sb.length());
    }
    if(root.left != null)
    {
    sb.append(root.val);
    sb.append("->");
    binaryTreePaths(root.left);
    sb.delete(cntr,sb.length());
    }
    if(root.right != null)
    {
    sb.append(root.val);
    sb.append("->");
    binaryTreePaths(root.right);
    sb.delete(cntr,sb.length());
    }
    return result;
    }
    }

Leave a Comment