Interview Question


Team: SQL Server / DPG Group
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

if modular difference of depth(left) and depth(right) is greater than 1 then tree is not balanced.

int difference(int height1, int height2)
{
   if( height1 > height2)
      return height1 - height2;
   else 
      return height2 - height1;
}

int isBalancedBinaryTree(node *root)
{
   int x=0,y=0;

   if(root == NULL)
      return 0;
   x= depth(root->left);
   y= depth(root->right);

   printf("Left height =%d right height=%d\n", x, y);
   if(difference(depth(root->left),depth(root->right)) > 1)
      return(0);
   else 
      return 1;
}

- Pavan October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

‌boolean balance(node* T,int* h)
{
  if(T == null)
   {
     *h = 0;
     return true;
   }
   
   int lh,rh;
   bool lr = balance(T->left,&lh);
   bool rr = balance(T->right,&rh);

   if(mod(lh- rh)>1))
      return false;

   *h = lh+rh+1;
   return lr&rr ;
 
}

- Anonymous October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static boolean isBalanced(TreeNode01 root) {
return ((maxDepth(root) - minDepth(root)) <= 1);
}

private static int maxDepth(TreeNode01 root) {
if (root == null) {
return -1;
}

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}

private static int minDepth(TreeNode01 root) {
if(root.right == null && root.left == null) {
return 0;
}
if(root.left == null && root.right != null) {
return 1 + minDepth(root.right);
}
if(root.right == null && root.left != null) {
return 1 + minDepth(root.left);
}
else {
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}

}

- BoredCoder October 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are quite a few methods to judge the balance of tree structure. Like AVL, red-black and so on.

- peter tang October 15, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More