Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public boolean isTreeBalanced(Node root) {
return (maxDepth(root) - minDepth(root)) <= 1;
}
public int maxDepth(Node root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.leftChild), maxDepth(root.rightChild));
}
public int minDepth(Node root) {
if (root == null) {
return 0;
}
return 1 + Math.min(minDepth(root.leftChild), minDepth(root.rightChild));
}
private class Node {
Node leftChild;
Node rightChild;
}
@YoungXiang

This comment has been minimized.

Copy link

YoungXiang commented Aug 29, 2017

Can't believe that's how easy it is... I thought too hard on how to calculate the depth of a tree node...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.