## Wednesday, January 6, 2016

### Print all the paths from root to every leaf

http://siyang2notleetcode.blogspot.com/2015/02/path-for-binary-tree.html
Print all the paths from root to every leaf in a binary tree
public static void main(String args[]){
Solution s = new Solution();
int n = 10;
TreeNode[] tree = new TreeNode[n];
for(int i = 0;i < n;i++)
tree[i] = new TreeNode(i);
for(int i = 0;2*i+2 < n;i++){
tree[i].left = tree[2*i+1];
tree[i].right = tree[2*i+2];
}
List<List<Integer>> list = s.pathsForBinaryTree(tree[0]);
for(int i = 0;i < list.size();i++)
System.out.println(list.get(i));

}

public List<List<Integer>> pathsForBinaryTree(TreeNode root){
// recursive DFS
List<List<Integer>> rlst = new ArrayList<List<Integer>>();
if(root==null){return rlst;}
search(new ArrayList<Integer>(), rlst, root);
return rlst;
}

private void search(List<Integer> list, List<List<Integer>> rlst, TreeNode root){
List<Integer> newList = new ArrayList<Integer>(list);
if(root.left==null && root.right==null)
else{
if(root.left!=null)
search(newList, rlst, root.left);
if(root.right!=null)
search(newList, rlst, root.right);
}
}

http://www.geeksforgeeks.org/given-a-binary-tree-print-all-root-to-leaf-paths/
Not good ==> have to create int path[] = new int[1000];
Better to use dynamic List
 `class` `Node {` `    ``int` `data;` `    ``Node left, right;` `    ``Node(``int` `item) {` `        ``data = item;` `        ``left = right = ``null``;` `    ``}` `}` `class` `BinaryTree {` `    ``static` `Node root;` `    `  `    ``/*Given a binary tree, print out all of its root-to-leaf` `     ``paths, one per line. Uses a recursive helper to do the work.*/` `    ``void` `printPaths(Node node) {` `        ``int` `path[] = ``new` `int``[``1000``];` `        ``printPathsRecur(node, path, ``0``);` `    ``}` `    ``/* Recursive helper function -- given a node, and an array containing` `     ``the path from the root node up to but not including this node,` `     ``print out all the root-leaf paths.*/` `    ``void` `printPathsRecur(Node node, ``int` `path[], ``int` `pathLen) {` `        ``if` `(node == ``null``) {` `            ``return``;` `        ``}` `        ``/* append this node to the path array */` `        ``path[pathLen] = node.data;` `        ``pathLen++;` `        ``/* it's a leaf, so print the path that led to here  */` `        ``if` `(node.left == ``null` `&& node.right == ``null``) {` `            ``printArray(path, pathLen);` `        ``} ``else` `{` `            `  `            ``/* otherwise try both subtrees */` `            ``printPathsRecur(node.left, path, pathLen);` `            ``printPathsRecur(node.right, path, pathLen);` `        ``}` `    ``}` `    ``/* Utility function that prints out an array on a line. */` `    ``void` `printArray(``int` `ints[], ``int` `len) {` `        ``int` `i;` `        ``for` `(i = ``0``; i < len; i++) {` `            ``System.out.print(ints[i] + ``" "``);` `        ``}` `        ``System.out.println(``""``);` `    ``}` `}`
http://www.geeksforgeeks.org/given-a-binary-tree-print-out-all-of-its-root-to-leaf-paths-one-per-line/
http://blog.gainlo.co/index.php/2016/04/15/print-all-paths-of-a-binary-tree/
Obviously, this is a tree traversal problem, which is quite common in coding interviews. You’d better be extremely familiar with algorithms like in-order, pre-order, post-order traversal, DFS, BFS etc..
In this problem, you can notice that the traversal order is to go deeper and deeper from each node and when it hits the leaf, it will go back and go deeper again. I would expect Depth-first search (DFS) to come to your mind immediately. In fact, the common mental model is that by analyzing the nature of the traverse problem, we come to DFS, instead of the other way around – try all the traversal algorithms to see which works.