check a tree is balanced or not
2012-06-06 13:34
399 查看
Question:
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.
Reference:
http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/ http://blog.theliuy.com/2012/balanced-binary-tree/
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/
相关文章推荐
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- Check if the given binary tree is BST or not.
- A program to check if a binary tree is BST or not
- Check whether a given Binary Tree is Complete or not 解答
- 判断是否为完全二叉树 Check whether a binary tree is a complete tree or not
- Geeks - Check whether a given graph is Bipartite or not 二分图检查
- VB.net check whether a file is opened or not
- Geeks - Check whether a given graph is Bipartite or not 二分图检查
- 第15题 To Check the given linked list is palindrome or not
- check a string is numeric or not? 检查字符串是否是数值型的
- dataStructure@ Check whether a given graph is Bipartite or not
- VSTO Check Cell is In Edit Mode or Not
- Q4.1 Implement a function to check if a tree is balanced
- Check an input positive interger is a prime number or not
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- *Check whether a given graph is Bipartite or not
- Check string is palindrome or not
- LeetCode题目笔记(四) -- Check if a binary tree is balanced
- Android Check GPS is Enabled or Not
- java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class