257. Binary Tree Paths

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

For example, given the following binary tree:

   1
 /   \
2     3
 \
  5

All root-to-leaf paths are:

["1->2->5", "1->3"]

Solution recursive

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    List<String> result = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null) return result;
        List<TreeNode> path = new ArrayList<>();
        buildPath(root, path);
        return result;
    }

    private void buildPath(TreeNode root, List<TreeNode> path){

        if(root.left == null && root.right == null){
            path.add(root);
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< path.size()-1; i++){
                TreeNode tn = (TreeNode)path.get(i);
                sb.append(tn.val);
                sb.append("->");
            }
            TreeNode last = (TreeNode)path.get(path.size()-1);
            sb.append(last.val);
            path.remove(root);
            result.add(sb.toString());
        }

        path.add(root);
        if(root.left != null){
            buildPath(root.left, path);
        }
        if(root.right != null){
            buildPath(root.right, path);
        }
        path.remove(root);
    }
}

A clear DFS

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {

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

        buildPath(root, String.valueOf(root.val));
        return res;
    }

    void buildPath(TreeNode root, String list){
        if(root.left == null && root.right == null){
            res.add(list);
            return;
        }
        if(root.left != null){
            buildPath(root.left, list + "->" + String.valueOf(root.left.val));
        }

        if(root.right != null){
            buildPath(root.right, list + "->" + String.valueOf(root.right.val));
        }
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> list = new ArrayList<>();

        print(root, list);
        return res;
    }

    void print(TreeNode root, List<Integer> list){
        if(root == null) return;
        list.add(root.val);
        if(root.left == null && root.right == null){

            StringBuilder sb = new StringBuilder();
            sb.append(list.get(0));

            for(int i=1; i<list.size(); i++){
                sb.append("->");
                sb.append(list.get(i));
            }
            res.add(sb.toString());
        }
        print(root.left, list);
        print(root.right, list);
        list.remove(list.size()-1);
    }
}

results matching ""

    No results matching ""