Domanda di colloquio di Meta

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

Risposte di colloquio

Anonimo

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.

Anonimo

4 mag 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); }