Amazon Interview Question
SDE-2sTeam: Hyderabad
Country: India
Interview Type: Phone Interview
def find_parent(root, val):
if not root:
return None
if root.left and root.left.data == val:
return root
if root.right and root.right.data == val:
return root
find_left = find_parent(root.left, val)
if find_left:
return find_left
find_right = find_parent(root.right, val)
if find_right:
return find_right
return None
So the Binary tree is not a sorted binary tree. In this case we have to perform a full scan:
public class Node
{
public Node Left { get; set; }
public Node Right { get; set; }
public int Value { get; set; }
public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture);
}
}
public class TreeSearcher
{
public static Node Search(Node parent, int valueToSearch)
{
return SearchInternal(parent, valueToSearch, null);
}
private static Node SearchInternal(Node n, int valueToSearch, Node parentNode)
{
if (n == null)
return null;
if (n.Value == valueToSearch)
return parentNode;
var val = SearchInternal(n.Left, valueToSearch, n);
return val ?? SearchInternal(n.Right, valueToSearch, n);
}
}
Original case:
static <T extends Comparable<T>> TreeNode<T> findParentForNodeInBT(TreeNode<T> root, T value) {
if (root == null) {
return null;
}
TreeNode<T> currentNode = root;
if (currentNode.val.compareTo(value) == 0) {
return null;
}
LinkedList<TreeNode<T>> queue = new LinkedList<>();
queue.add(currentNode);
while (!queue.isEmpty()) {
currentNode = queue.pollFirst();
// follow the right branch (if possible)
if (currentNode.right != null) {
if (currentNode.right.val.compareTo(value) == 0) {
return currentNode;
}
queue.add(currentNode.right);
}
// follow the left branch (if possible)
if (currentNode.left != null) {
if (currentNode.left.val.compareTo(value) == 0) {
return currentNode;
}
queue.add(currentNode.left);
}
}
return null;
}
If the binary tree is a binary search tree:
static <T extends Comparable<T>> TreeNode<T> findParentForNodeInBST(TreeNode<T> root, T value) {
if (root == null) {
return null;
}
TreeNode<T> currentNode = root;
if (currentNode.val.compareTo(value) == 0) {
return null;
}
while (true) {
// follow the right branch (if possible)
if (currentNode.val.compareTo(value) < 0 && currentNode.right != null) {
if (currentNode.right.val.compareTo(value) == 0) {
return currentNode;
}
currentNode = currentNode.right;
continue;
}
// follow the left branch (if possible)
if (currentNode.val.compareTo(value) > 0 && currentNode.left != null) {
if (currentNode.left.val.compareTo(value) == 0) {
return currentNode;
}
currentNode = currentNode.left;
continue;
}
break;
}
return null;
}
Can be done using PreOrder Traversal as shown below. capture the immediate ancestor when function stack is unwinding after having found the key
private boolean findParent(Node root, int x) {
if(root == null) return false;
if(root.data == x )return true;
if(findParent(root.left,x) || findParent(root.right,x)){
System.out.print(root.data);
return false;
}
return false;
}
public static TreeNode findXParent(TreeNode root, int x){
if(root == null)return null;
if(root.value == x)return null;
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.add(root);
while(!que.isEmpty()){
TreeNode temp = que.remove();
if(temp.left != null){
if(temp.left.data == x)return temp;
que.add(temp.left);
}
if(temp.right != null){
if(temp.right.data == x)return temp;
que.add(temp.right);
}
}
return null;
}
Sweet PostOrder in Recursion C#
private int SearchResult { get; set; }
public void FindItsParent(BinaryTree<int> tree, int elementToBeFound, int Parent)
{
if (tree == null) return;
FindItsParent(tree.Left, elementToBeFound, tree.Node);
FindItsParent(tree.Right, elementToBeFound, tree.Node);
if (tree.Node == elementToBeFound)
{
SearchResult = Parent;
}
}
Node getParent(final Node root, final int data) {
Node found = null;
if (root != null) {
if (root.left != null) {
if (root.left.data == data) {
return root;
}
}
if (root.right != null) {
if (root.right.data == data) {
return root;
}
}
found = getParent(root.left, data);
if (found == null) {
found = getParent(root.right, data);
}
}
return found;
}
static void findParent(Node node,
int val, int parent)
{
if (node == null)
return;
// If current node is the required node
if (node.data == val)
{
// Print its parent
System.out.print(parent);
}
else
{
// Recursive calls for the children
// of the current node
// Current node is now the new parent
findParent(node.left, val, node.data);
findParent(node.right, val, node.data);
}
}
static void findParent(Node node,
int val, int parent)
{
if (node == null)
return;
// If current node is the required node
if (node.data == val)
{
// Print its parent
System.out.print(parent);
}
else
{
// Recursive calls for the children
// of the current node
// Current node is now the new parent
findParent(node.left, val, node.data);
findParent(node.right, val, node.data);
}
}
Node* returnParent(Node* root, int val)
{
if (!root)
return NULL;
if ((root->left != NULL && root->left->val == val) || (root->right != NULL && root->right->val == val))
return root;
else
{
Node* l = returnParent(root->left, val);
Node* r = returnParent(root->right, val);
if (l != NULL)
return l;
return r;
}
}
breadth traversal approach; also can be implemented with Deep traversal like pre-order
- hnatsu January 18, 2017