**Search in a Virtually Complete Binary Tree- Google Top Interview Questions**

### Problem Statement :

Consider a complete binary tree of n nodes whose values are 1 to n. The root has value of 1, its left child is 2 and its right child is 3. In general, nodes' values are labelled 1 to n in level order traversal. You are given a binary tree root and an integer target. Given that the root was originally a complete binary tree whose values were labelled as described above, and that some of the subtrees were deleted, return whether target exists in root. Bonus: solve in \mathcal{O}(h)O(h) time where h is the height of the tree. Constraints 1 ≤ n ≤ 100,000 where n is the number of nodes in root Example 1 Input root = [1, [2, [4, null, null], null], [3, [6, null, null], [7, null, null]]] target = 6 Output True Explanation 6 exists in this tree. Example 2 Input root = [1, [2, [4, null, null], null], [3, [6, null, null], null]] target = 7 Output False Explanation 7 does not exist in this tree.

### Solution :

` ````
Solution in C++ :
*/
bool check(Tree* root, vector<int>& path) {
while (root) {
if (root->val != path.back()) return false;
path.pop_back();
if (path.empty()) return true;
if (path.back() % 2)
root = root->right;
else
root = root->left;
}
return false;
}
bool solve(Tree* root, int target) {
if (target < 1) return false;
vector<int> path{target};
for (int i = target; i != 1;) {
i /= 2;
path.push_back(i);
}
return check(root, path);
}
```

` ````
Solution in Java :
import java.util.*;
/**
* public class Tree {
* int val;
* Tree left;
* Tree right;
* }
*/
class Solution {
public boolean solve(Tree root, int target) {
if (target <= 0)
return false;
// we can construct the path to target
LinkedList<Integer> path = new LinkedList();
int tmp = target;
while (tmp != 1) {
path.addFirst(tmp);
tmp = tmp / 2;
}
Tree trav = root;
while (path.size() > 0) {
int num = path.removeFirst();
if (num % 2 == 0) {
if (trav.left == null)
return false;
trav = trav.left;
} else {
if (trav.right == null)
return false;
trav = trav.right;
}
}
return true;
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, root, target):
if target == 0:
return False
path = []
while target > 1:
if target % 2:
path.append(1)
else:
path.append(-1)
target //= 2
for x in path[::-1]:
if x == -1:
if root.left is None:
return False
root = root.left
else:
if root.right is None:
return False
root = root.right
return True
```

## View More Similar Problems

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →