LexisNexis Interview Question






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

Find the depth of the tree recursively. On the way back compare the depth of left tree and right tree if they differ by more than one then you have an unbalanced tree.

- Anonymous November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

not exactly...
even if the depth differ by one, they may be unbalanced...
Consider the following tree


------A------
----B---C----
--D--E-F-----
G------------


This tree is not balanced however the difference between the leaf nodes is 1. Hence we need to find some other logic.

- vinaysachdeva23 November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

as defined in the problem the tree u mentioned is balanced all the leafs have distances from root either 2 or 3 which differs max 1 (is'nt that)

- Anonymous November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, we can count the total number of nodes in the tree and calculate the depth of the tree.
If 2^depth >total nodes>2^(depth-1), then the tree will be balanced.
But I am not sure....
Anyone has any other method??

- Anonymous November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

first sol is fine :)

- Anonymous November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

r u sure?
I think we have to use both first and second solutions i.e the difference between the depths should not be more than 2 and 2^depth >total nodes>2^(depth-1)
then only it will be balanced.
I think the above tree is not balanced

- Anonymous November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think the above tree is balanced ...
trees with a worst-case height of log(n) are called balanced trees...and it is also written in the questions" the level diff should not greater than one at each node .i think this is the only def of balanced tree..hey name mate :) , plz correct me if i am wrong.. thanks

- Anonymous November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what kind of tree is it? and how is the tree stored?

- chennavarri November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is just a binary tree, not necessarily a BST.

- riderchap November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can check the difference of height of left and right subtree at each node, you can say it is not balanced if diff is more than one at any node.

it will be done in O(n logn)

- Mogambo November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think that checking the difference between left and right subtree at every node might not work - I can't quite come up with a counter-example though.

What probably needs to be done is to find the max height of the tree and then the min height and then check that the difference is <= 1. Max height == the height of the leaf node that's furthest away from the root; min height == height of the leaf node that's closest to the root.

- Anonymous November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

when you go from bottom to top(by recursion) you do not need to check max or min hight at every node just check whether the height difference is |1| or not...i think first solution is enough...

- Anonymous November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a counter example is:

A
              /    \
            /        \
          /            \
       B              C
      /   \            /   \
     D  E         F    G
    /  \    \          \
  H   I    J          K
     \
      L

In this case, above mentioned algo gives a balanced tree. This is a balanced tree by normal definition but this isn't one according to the given question.

- Tushar June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry for the editing
i meant the tree:
---------------A-----------
---------B---------C------
------D---E----F---G---
----H--I----J----K--------
---L------------------------

Level Difference between roots L and G is 2 (>1)

- Tushar June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

MaxDepth - MinDepth should be < 1 for tree to be balanced.
Both can easily be found using a recursive method.

// return Max(node.left + 1, node.right + 1);

- Metta November 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is for max depth right?
How do we find the min depth?

- Kannan November 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is the correct ans

- Anonymous January 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about the next solution?

public static int getMaxDepth(Node root)
{
	if(root==null) return 0;
	return 1+Max(getMaxDepth(root.left),getMaxDepth(root.right));
}
public static int getMinDepth(Node root)
{
	if(root==null) return 0;
	return 1+Min(getMinDepth(root.left),getMinDepth(root.right));
}
public static boolean isBalanced(Node root)
{
	if(getMaxDepth(root)-getMinDepth(root)<=1)
	{
		return true;
	}
	return false;
}

- wsgis July 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

-----------------A-----------------
---------B--------------C---------
---- D-----E-------F-----G-----
---H--I------J-------K-------M--
--L--------------------------------

If we have this tree, then it is balanced. But, MinDepth of A will be calculated as 3 whereas MaxDepth as 5. So it will give it as an unbalanced tree.
Please correct me if I am taking this incorrectly.

- Tushar June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this should work.

struct TN{
int data;
struct TN* left;
struct TN* right;
};

///////////////////////////////////////////////////////////////
//returns length of longest path from root to a leaf
int getHeight(TN *root)
{
	if(root == NULL)
	return 0;
	return(1 + max(getHeight(root->left), getHeight(root->right)));
}

/////////////////////////////////////////////////////////////
//returns length of minimum path from root to a leaf
int getMinHeight(TN *root)
{
	if(root == NULL)
	return 0;
	int lMinHeight = getMinHeight(root->left);
	int rMinHeight = getMinHeight(root->right);
	if(lMinHeight == 0 || rMinHeight == 0)
	{return 1+max(lMinHeight,rMinHeight);
	}
	else
	{return 1 + min(lMinHeight, rMinHeight);}
}

////////////////////////////////////////////////////////
//function to check balance of tree
bool checkBalance(TN *root)
{
	int maxHeight = getHeight(root);
	int minHeight = getMinHeight(root);
	
	if(maxHeight - minHeight > 1)
	return false;
	else
	return true;
}

- Tushar June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

bool IsBalancedOrNot(Node *root, int &height)
{
  if (root == NULL) { 
     height = 0; 
     return true; 
  }
  
  int lHeight = 0, rHeight = 0;  
  if (IsBalancedOrNot(root->left, &lHeight) &&   IsBalancedOrNot(root->right, &rHeight)
  {
    height = max(lHeight, rheight) + 1;        
    return ( abs(lHeight-rHeight) <=1 );
  }
  else
    return false;
}

- r.ramkumar December 16, 2010 | 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