Wednesday, August 19, 2015

Leetcode: 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"]
Link to the another post on the same question:
http://buttercola.blogspot.com/2014/11/facebook-print-all-paths-from-root-to.html

Code (Java):
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int[] levelArray = new int[1000];
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result  = new ArrayList<String>();
        if (root == null) {
            return result;
        }
        
        binaryTreePathsHelper(root, 0, result);
        
        return result;
    }
    
    private void binaryTreePathsHelper(TreeNode root, int level, List<String> result) {
        if (root == null) {
            return;
        }
        
        levelArray[level] = root.val;
        
        if (root.left == null && root.right == null) {
            outputToResult(result, level);
        }
        
        binaryTreePathsHelper(root.left, level + 1, result);
        binaryTreePathsHelper(root.right, level + 1, result);
    }
    
    private void outputToResult(List<String> result, int level) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < level; i++) {
            sb.append(levelArray[i] + "->");
        }
        sb.append(levelArray[level]);
        result.add(sb.toString());
    }
}

Update on 10/22/15:
There is another method without needing to use a level array. 

Code (Java):
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        
        if (root == null) {
            return result;
        }
        
        List<Integer> curr = new ArrayList<>();
        
        binaryTreePathsHelper(root, curr, result);
        
        return result;
    }
    
    private void binaryTreePathsHelper(TreeNode root, List<Integer> curr, List<String> result) {
        if (root == null) {
            return;
        }
        
        curr.add(root.val);
        
        // If is the leaf
        if (root.left == null && root.right == null) {
            StringBuffer sb = new StringBuffer();
            if (curr.size() == 1) {
                sb.append(curr.get(0));
                result.add(sb.toString());
            } else {
                for (int i = 0; i < curr.size() - 1; i++) {
                    sb.append(curr.get(i));
                    sb.append("->");
                }
                
                sb.append(curr.get(curr.size() - 1));
                result.add(sb.toString());
            }
            
            return;
        }
        
        if (root.left != null) {
            binaryTreePathsHelper(root.left, curr, result);
            curr.remove(curr.size() - 1);
        }
        
        if (root.right != null) {
            binaryTreePathsHelper(root.right, curr, result);
            curr.remove(curr.size() - 1);
        }
    }
}

No comments:

Post a Comment