patterncsharpMinor
Determine if a binary tree is balanced
Viewed 0 times
balancedbinarytreedetermine
Problem
I'm currently running through some Cracking the Coding Interview questions and my solution to 4-4 is quite different from any of the given solutions.
The question asks to determine if a given binary tree is balanced; more specifically, if the difference between the maximum and minimum depths within the tree is less than or equal to 1.
This works for my test cases, but they were far from exhaustive and I can't really conclude if my solution works for all cases / is efficient. My solution feels far too simple, which leads me to believe I've missed some kind of gotcha.
The question asks to determine if a given binary tree is balanced; more specifically, if the difference between the maximum and minimum depths within the tree is less than or equal to 1.
public static bool IsBalanced(Node node)
{
var mm = new DepthMinMax();
mm.Min = int.MaxValue;
mm.Max = int.MinValue;
FindMinMax(mm, node, 0);
return (mm.Max - mm.Min depth) mm.Min = depth;
if (mm.Max < depth) mm.Max = depth;
}
}
public class Node
{
public Node L { get; set; }
public Node R { get; set; }
}
public class DepthMinMax
{
public int Min { get; set; }
public int Max { get; set; }
}This works for my test cases, but they were far from exhaustive and I can't really conclude if my solution works for all cases / is efficient. My solution feels far too simple, which leads me to believe I've missed some kind of gotcha.
Solution
Came across this post as I was answering the same question in this book. My approach was also similar in solving this. I will post what I found
as an answer since I cannot comment.
First, I think the
node is incorrect. It should be a
updating depths.
For the following tree this would give a max depth of 3 and min depth of 1, which is incorrect. I did this in C++ as shown below.
Second, I am not sure whether there are differences in the versions.
In my book the question 4.1 is about checking whether a binary tree is balanced. This is defined as
heights of the two subtrees of any node never differ by more than one
If this is the requirement I think this algorithm won't work.
Check the first two answers of this question. I think this algorithms checks whether the difference between farthest and closest leaves from root differ by more than one.
Edit: Here I assumed min max corresponds to maximum and minimum depths to leaf nodes.
as an answer since I cannot comment.
First, I think the
or condition for checking the terminating node is incorrect. It should be a
and condition to my understanding. I think we need to identify whether the current node is a leaf node before updating depths.
For the following tree this would give a max depth of 3 and min depth of 1, which is incorrect. I did this in C++ as shown below.
5
/ \
4 7
/ / \
1 6 8
/
0
void FindMinMax(int &min,int &max,Node *np,int height)
{
if(np==NULL) return;
FindMinMax(min,max,np->left,height+1);
FindMinMax(min,max,np->right,height+1);
if(np->left==NULL || np->right==NULL)
{
if(min>height) min = height;
if(max<height) max = height;
}
}Second, I am not sure whether there are differences in the versions.
In my book the question 4.1 is about checking whether a binary tree is balanced. This is defined as
heights of the two subtrees of any node never differ by more than one
If this is the requirement I think this algorithm won't work.
Check the first two answers of this question. I think this algorithms checks whether the difference between farthest and closest leaves from root differ by more than one.
Edit: Here I assumed min max corresponds to maximum and minimum depths to leaf nodes.
Code Snippets
5
/ \
4 7
/ / \
1 6 8
/
0
void FindMinMax(int &min,int &max,Node *np,int height)
{
if(np==NULL) return;
FindMinMax(min,max,np->left,height+1);
FindMinMax(min,max,np->right,height+1);
if(np->left==NULL || np->right==NULL)
{
if(min>height) min = height;
if(max<height) max = height;
}
}Context
StackExchange Code Review Q#106673, answer score: 5
Revisions (0)
No revisions yet.