LeetCode 110 | Balanced Binary Tree | Solution Explained (Java + Whiteboard)

preview_player
Показать описание
Running Time: O(N)
Space Complexity: O(nlog(n)

FOLLOW ME:
Рекомендации по теме
Комментарии
Автор

Thanks for that extra recursion step... I was so frustrated...
202/227 test case passed and i cannot find out the case why it was happening... Thanx man... Nice video... Keep ur good work...

surajsaha
Автор

Your explanation is great brother. Please keep up the great work and continue teaching us.

ashimkarki
Автор

it's enough to find one unbalanced subTree to say that the whole tree is unbalanced
class Solution {
boolean isBalanced = true;


public boolean isBalanced(TreeNode root) {
if (root == null) return true;
dfs(root);
return isBalanced;
}

private int dfs(TreeNode root) {
if (root == null) return -1;
int left = dfs(root.left);
int right = dfs(root.right);
if (Math.abs(left - right) > 1) {
isBalanced = false;
}

return Integer.max(left + 1, right + 1);
}

}

gulfstream
Автор

It sounds like you need to take your dog out.

HenggaoCai