Given a binary tree, check whether it is balanced or not.
Analyze:
Consider a height-balancing scheme where following conditions should be checked to determine if a binary tree is balanced.
An empty tree is height-balanced. A non-empty binary tree T is balanced if:
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.
The above height-balancing scheme is used in AVL trees. The diagram below shows two trees, one of them is height-balanced and other is not. The second tree is not height-balanced because height of left subtree is 2 more than height of right subtree.
To check if a tree is height-balanced, get the height of left and right subtrees. Return true if difference between heights is not more than 1 and left and right subtrees are balanced, otherwise return false.
Code:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
if (Math.abs(height(root.left) - height(root.right)) > 1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
public int height(TreeNode root) {
if (root == null) return 0;
return Math.max(height(root.left), height(root.right)) + 1;
}
}
The complexity of the code above is O(N^2). However, some interviewers may ask to provide solution with O(N) complexity. Below is the solution with O(N) complexity. The main idea is if the subtree is balanced, return the true height, else, return -1. Therefore, the parent node of that unbalanced subtree also has -1 height.
public class Solution {
public boolean isBalanced(TreeNode root) {
return getDepth(root) >= 0;
}
public int getDepth(TreeNode node) {
if (node == null) return 0;
int leftDepth = getDepth(node.left);
if (leftDepth < 0) return -1;
int rightDepth = getDepth(node.right);
if (rightDepth < 0) return -1;
if (Math.abs(leftDepth - rightDepth) > 1) return -1;
return Math.max(leftDepth, rightDepth) + 1;
}
}
Reference:
http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/
http://blog.theliuy.com/2012/balanced-binary-tree/
Read More:
- Pull is not possible because you have unmerged files. Please, fix them up in the work tree, and then
- When using ionic to build Android APK, Cordova error is reported: requirements check failed for JDK 1.8 or greater
- Binary tree traversal (preorder, middle order, postorder, hierarchy traversal, depth first, breadth first)
- The media could not be loaded, either because the server or network failed or because the format is
- Server check fail, please check server 192.168.11.13 ,port 9848 is available , error ={}
- Failed to load viewstate.The control tree into which viewstate is being loaded must match the contro…
- In windows, “cmake” is not an internal or external command, nor a runnable program or batch file.
- After node.js is installed, use the instruction node version in vscode to show that it is not an external or internal instruction. The solution is as follows:
- Version 1.8.0_201 of the JVM is not suitable for this product. Version: 11 or greater is required.
- Ant Design ‘cross env’ is not an internal or external command, nor is it an error reporting problem for a runnable program
- [How to Fix] fatal git-write-tree error building trees
- Error in installing torch vision or pilot on Linux or Jetson nano: the headers or library files could not be found for JPEG
- Traversing the background data to generate tree structure
- 【.Net Core】using declarations‘ is not available in C# 7.3. Please use language version 8.0 or greate
- Net prompt is not an internal or external command
- ‘builtin_ function_ or_ Method ‘object is not subscriptable error
- Viewing Android dependency tree using gradle
- Unable to resolve dependency tree
- Virtual environment: error: virtualenv is not compatible with this system or executable
- [screen recording] an error is reported in the screenrecord command: inaccessible or not found