Microsoft Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
Small modification:
if ((sum == 0) && (root == null))
return true;
else if (root == null)
return false;
else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}
> This has exponential time complexity
No, it doesn't. There are no loops, and one call per node.
@pal :
think ur code requires little modification ..
consider the below tree
4
/ \
6 8
\ /
5 1
required sum is 10 .. such path doesnt not exist , but ur program returns true ..
Yes, it has exponential complexity. For each function call you have two function calls.
Hence its complexity is O(2^n).
@msankith
yeah ... you are right ..
I guess this will do ..
bool IsSum (root, sum) {
if(root == null)
return;
if ((sum == root->val) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;
else if ((root->left == null) && (root->right == null)) // reached leaf node but sum not found
return false;
else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}
@pal : another modification
if ((sum == root->val) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;
should be
if ((sum == 0) && (root->left == null) && (root->right == null)) //reached root and sum is found
return true;
How about this ?
/* check if leaf node exists and sum from root to given leaf node = given sum */
public boolean checkLeafExistsAndSumFromRootIsGivenSum (Object k, int checkSum) throws NoRootException {
/* check if root is present */
if(getRoot()==null) throw new NoRootException();
/* boolean present to return */
boolean isPresent = false;
/*sum to sum all items */
int sum=0;
/*node to store root */
BinarySearchTreeNodeList node = getRoot();
/* loop untill null is reached */
while(node!=null){
/* compare item with object to search */
int c = (((Comparable)k).compareTo(node.getItem()));
/* if k > node.item */
if(c>0){
/* if node.right exists then move right else no node exists; return false and stop */
if(node.getRight()!=null){
sum=sum (Integer)node.getItem();
node=node.getRight();
isPresent=true;
}else{
isPresent=false;break;
}
}else if(c<0){ /* if k < node.item */
/* if node.left exists move left else no node exists;return false and stop; */
if(node.getLeft()!=null){
sum=sum+(Integer)node.getItem();
node=node.getLeft();
isPresent=true;
}else{
isPresent=false;break;
}
}else if(c==0){
/* if k==node.item then check if sum== check sum else return false and stop; */
sum=sum+(Integer)node.getItem();
if(sum==checkSum)
isPresent=true;
else isPresent=false;
break;
}
}
return isPresent;
}
D(n, k)
if(k == 0 && n == NULL)
return true
if(k!=0 && n == NULL)
return false
if(k < 0)
return false
return D(n->l, k - n->l->d) || D(n->r, k - n->r->d)
Elegantly done using DP:
maintain OPT( node, k ) for each node such that OPT( node, k ) = true if some path starting from this node has the sum k. compute from Bottom up from leaf upwards
i.e. OPT( node, k ) = true if OPT( node->left, k - node->value ) = true || OPT( node->right, k - node->value ) = true. O(nk)
bool IsSum (root, sum) {
- pal October 31, 2011if(sum == 0)
return true;
else if (root == null)
return false;
else
return (IsSum(root->left, sum - root->val) ||
IsSum(root->right, sum - root->val) )
}