Microsoft Interview Question
Software DevelopersCountry: United States
Interview Type: In-Person
int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
if(root == null)
return 0;
int count == 0;
if(root.val == attribValue)
{
if(currentMax == NULL)
currentMax = root;
count++;
}
TreeNode* max = currentMax;
if(root->left != NULL)
{
TreeNode* maxLeft = currentMax;
int leftCount = getMax(root->left, maxLeft, attribValue);
if(currentMax == maxLeft)
count = count + leftCount;
else
{
if(leftCount > count)
currentMax = maxLeft;
count = max(count, leftCount);
}
}
if(root->right != NULL)
{
TreeNode* maxRight = max;
int rightCount = getMax(root->right, maxRight, attribValue);
if(currentMax == maxRight)
count = count + rightCount;
else
{
if(rightCount > count)
currentMax = maxRight;
count = max(count, rightCount);
}
}
return count;
}
int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
if(root == null)
return 0;
int count == 0;
if(root.val == attribValue)
{
if(currentMax == NULL)
currentMax = root;
count++;
}
TreeNode* max = currentMax;
if(root->left != NULL)
{
TreeNode* maxLeft = currentMax;
int leftCount = getMax(root->left, maxLeft, attribValue);
if(currentMax == maxLeft)
count = count + leftCount;
else
{
if(leftCount > count)
currentMax = maxLeft;
count = max(count, leftCount);
}
}
if(root->right != NULL)
{
TreeNode* maxRight = max;
int rightCount = getMax(root->right, maxRight, attribValue);
if(currentMax == maxRight)
count = count + rightCount;
else
{
if(rightCount > count)
currentMax = maxRight;
count = max(count, rightCount);
}
}
return count;
}
int getMax(TreeNode root, TreeNode* currentMax, enum attribValue)
{
if(root == null)
return 0;
int count == 0;
if(root.val == attribValue)
{
if(currentMax == NULL)
currentMax = root;
count++;
}
TreeNode* max = currentMax;
if(root->left != NULL)
{
TreeNode* maxLeft = currentMax;
int leftCount = getMax(root->left, maxLeft, attribValue);
if(currentMax == maxLeft)
count = count + leftCount;
else
{
if(leftCount > count)
currentMax = maxLeft;
count = max(count, leftCount);
}
}
if(root->right != NULL)
{
TreeNode* maxRight = max;
int rightCount = getMax(root->right, maxRight, attribValue);
if(currentMax == maxRight)
count = count + rightCount;
else
{
if(rightCount > count)
currentMax = maxRight;
count = max(count, rightCount);
}
}
return count;
}
TreeNode getMax(TreeNode root, TreeNode currentMax, enum attribValue){
if(root == null)
return currentMax;
if ((root->left.val == attribValue) && (root->left.val == attribValue)) {
getMax(root->left, currentMax, attribValue);
getMax(root->right, currentMax, attribValue);
} else {
getMax(root->left, root->left, attribValue);
getMax(root->right, root->right,, attribValue);
}
}
- Debatri April 01, 2018