Facebook Interview Question
Software Engineer / DevelopersTeam: Backend systems
Country: United States
Interview Type: Phone Interview
void print_LL(Node* node){//written in c++
queue<Node*> curr;
if(node == NULL) return;
curr.push(node);
int k= curr.size();
while(!curr.empty()){
Node *node = curr.front();
cout<<node->data<<" ";
curr.pop();
k--;
if(node->left != NULL) curr.push(node->left);
if(node->right != NULL) curr.push(node->right);
if(k==0 && !curr.empty()) {k=curr.size(); cout<<endl;}
}
}
queue.push(head);
queue.push("<delimiter>");
while(queue.size != 0)
{
elem = queue.pop();
if( elem == "<delimiter>" && queue.size() != 0) {
queue.push("<delimiter>");
continue;
}
if(elem->left) queue.push(elem->left);
if(elem->right) queue.push(elem->right);
}
if tree is as below:
50
30 60
20 40 55 70
then it will be as follows:
1) 50->"<delim>"
2) print 50, enque 30, 60, queue looks like "<delim>"->30->60
3) print "<delim>", enqueue "<delim>", queue looks like 30->60->"<delim>"
4) print 30, enqueue 20, 40 queue looks like 60->"<delim>"->20->40
5) print 60, enqueue 55, 70 queue looks like "<delim>"->20->40->55->70
6) print "<delim>", enqueue "<delim>" queue looks like 20->40->55->70->"<delim>"
7) since they are leaf nodes, we will just pop them off
so output is: 50, " ", 30,60, " ", 20,40,55,70, " "
Pseudocode
printTreeLevels(Node n):
nextLevel = []
printLevel(nextLevel.add(n),0)
printLevel(Node[] nodes, int level):
if(nodes == null || nodes.size == 0):
return;
print "Level : " + level;
nextLevel = []
for(Node n: nodes):
print n.name;
nextLevel.add(n.left) if n.left != null;
nextLevel.add(n.right) if n.right != null
print "\n"
printLevel(nextLevel, level + 1)
Your nickname is great! *rofl*
Okay this has gotten way uglier than I anticipated but except for the printTree function the code doesn't really matter...
#include <string>
#include <vector>
#include <functional>
#include <memory>
template<class TTree>
void printTree(
TTree tree,
std::function<void (TTree, std::function<void (TTree)>)> enumChildren,
std::function<void(TTree)> printNode,
std::function<void()> printLevelSepa)
{
std::vector<TTree> frontier;
frontier.push_back(tree);
frontier.push_back(nullptr);
for(int i = 0; i < frontier.size(); i++)
{
auto current = frontier[i];
if(current == nullptr)
{
printLevelSepa();
if(i < frontier.size() - 1) // only if there are children left to process
frontier.push_back(nullptr); // signal end of next tree level
}
else
{
printNode(current);
enumChildren(current, [&](TTree child) { frontier.push_back(child); });
}
}
}
class BinaryTree
{
public:
typedef std::shared_ptr<BinaryTree> pointer_type;
pointer_type left;
pointer_type right;
std::string value;
BinaryTree(std::string value) : value(value), left(nullptr), right(nullptr) { }
BinaryTree(std::string value, pointer_type left, pointer_type right) : value(value), left(left), right(right) { }
};
int main(int argc, char** argv)
{
auto tree = std::make_shared<BinaryTree>(
"Root",
std::make_shared<BinaryTree>(
"L1_left",
std::make_shared<BinaryTree>("L1L_left"),
std::make_shared<BinaryTree>("L1R_right")
),
std::make_shared<BinaryTree>(
"R1_right",
std::make_shared<BinaryTree>("R1L_left"),
std::make_shared<BinaryTree>("R1R_right")
)
);
printTree<BinaryTree::pointer_type>(
tree,
[&](BinaryTree::pointer_type node, std::function<void (BinaryTree::pointer_type)> callback)
{
if(node->left != nullptr) callback(node->left);
if(node->right != nullptr) callback(node->right);
},
[&](BinaryTree::pointer_type node) { std::cout << node->value << " "; },
[&]() { std::cout << std::endl; });
return 0;
}
private static void PrintTreeLevelOrderQueue(TreeNode root)
{
var q = new Queue<TreeNode>();
q.Enqueue(root);
var currentCount = 1;
var nextCount = 0;
while(q.Count > 0)
{
var currentNode = q.Dequeue();
Console.Write(currentNode.Value + " ");
currentCount -= 1;
if (currentNode.Left != null)
{
q.Enqueue(currentNode.Left);
nextCount += 1;
}
if (currentNode.Right != null)
{
q.Enqueue(currentNode.Right);
nextCount += 1;
}
if(currentCount == 0)
{
Console.WriteLine();
currentCount = nextCount;
nextCount = 0;
}
}
}
LevelPrint(node* T )
{
if( T==NULL )return;
queue<node*>Q ;
Q.enqueue(T);
Q.enqueue(NULL);
while(!Q.empty())
{
node* t = Q.dequeue();
if( t!=NULL )
{
if(t->left)
Q.enqueue(t->left);
if(t->right)
Q.enqueue(t->right);
print(t);
}
else
{
Q.enqueue(NULL);
}
}
}
This code will never end. You need to ensure that you do not add any more NULLs to the queue once all nodes have been traversed.
void Util::PrintLayers(BTree* RootNode)
- Ashish January 17, 2012{
queue<BTree*> myqueue;
myqueue.push(RootNode);
BTree* tempNode = RootNode;
int OldLevel = 1;
int NewLevel = 0;
while(myqueue.size() != 0)
{
tempNode = myqueue.front();
cout<<"\t"<<tempNode->Value;
if(tempNode->Left != 0)
{
myqueue.push(tempNode->Left);
NewLevel++;
}
if(tempNode->Right != 0)
{
myqueue.push(tempNode->Right);
NewLevel++;
}
myqueue.pop();
OldLevel--;
if(OldLevel == 0)
{
OldLevel = NewLevel;
NewLevel = 0;
cout<<endl;
}
}
}