Facebook Interview Question
SDE1sCountry: United States
We use BFS to traverse the tree while keeping track on each child's parent
The same can be coded using recursion. As the question stated: this method fixes only one broken node (it can be adjusted to continue and scan for more double links):
#include "CommonHeader.h"
class BSTNode
{
public:
std::string label;
std::vector<BSTNode*> children;
BSTNode(const std::string& n)
: label(n)
{
}
BSTNode() {}
};
static std::unordered_map<std::string, BSTNode> G;
// Our test tree
// (a)
// / \
// / \
// (b) (c)
// | /
// | /
// (d)
BSTNode* makeTestTree()
{
G["a"] = BSTNode("a");
G["b"] = BSTNode("b");
G["c"] = BSTNode("c");
G["d"] = BSTNode("d");
G["a"].children.push_back(&G["b"]);
G["a"].children.push_back(&G["c"]);
G["c"].children.push_back(&G["d"]); // double parent
G["b"].children.push_back(&G["d"]); // double parent
return &G["a"];
}
void fixDoubleParentNode()
{
BSTNode* bst = makeTestTree();
// Keep track of the parents
std::unordered_map<BSTNode*, BSTNode*> Parents;
// Use BFS with Queue (we can apply the same logic with recursion)
std::queue<BSTNode*> q;
q.push(bst);
Parents[bst] = nullptr;
while(!q.empty()) {
BSTNode* node = q.front();
q.pop();
for(size_t i = 0; i < node->children.size(); ++i) {
BSTNode* child = node->children[i];
if(Parents.count(child)) {
// already visited this child
node->children.erase(node->children.begin() + i);
std::cout << "Found duplicate parenting for node: " << child->label
<< " faulty parent: " << node->label << " is removed" << std::endl;
return;
} else {
Parents[child] = node;
q.push(child);
}
}
}
}
public class FixBst
{
public static void main(String args[])
{
TreeNode root= new TreeNode(3);
root.left = new TreeNode(1);
root.right = new TreeNode(6);
root.left.right = new TreeNode(2);
root.right.left = root.left.right;
FixBst f = new FixBst();
f.print(root);
f.fix(root, Integer.MIN_VALUE, Integer.MAX_VALUE, null);
System.out.println();
f.print(root);
}
public void fix(TreeNode root, int min, int max, TreeNode parent)
{
if(root==null)
return;
if(root.val > min && root.val <= max)
{
fix(root.left, min, root.val, root);
fix(root.right, root.val, max, root);
}
else
{
if(parent.left.val == root.val)
parent.left = null;
else if(parent.right.val == root.val)
parent.right = null;
}
}
private void print(TreeNode root)
{
if(root==null)
return;
print(root.left);
System.out.print(root.val+"\t");
print(root.right);
}
}
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int val)
{
this.val = val;
}
}
Since it's not a regular binary tree, but a binary search tree, and taking in account that parent 1 and 2 are not ancestors (meaning that the parents are located in two different subtrees), having 2 parents breaks the property of a binary search tree. So, we can just check if the child's value satisfies the property or not.
In the code below, I assume that in the BST, left child's value must be less, and the right child's value must be greater or equal to the value of the parent.
- Alex October 02, 2017