Meta interview question

Given a binary tree, print all root-to-leaf paths

Interview Answers

Anonymous

21 Apr 2015

I said dfs, I used recursive programming. But I just could not make the code working, and I could not understand where to push/ pop the data from the stack in my implementation.

Anonymous

4 May 2015

It's similar to DFS! public void printAllPaths(Node node, int[] path, int len) { if (node == null) return; path[len] = node.mData; len++; if (node.mLeftNode == null && node.mRightNode == null) { for (int i = 0; i < len; i++) { System.out.print(path[i] + " "); } return; } printAllPaths(node.mLeftNode, path, len); System.out.println(); printAllPaths(node.mRightNode, path, len); }