Title:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree was defined as a binary tree in which the depth of the Every node never differ by more than 1.
Test Instructions:
Given a binary tree, determine whether it is a balanced binary tree.
Algorithm Analysis:
* Balanced binary trees (Balanced binary tree) are also known as AVL trees (unlike the AVL algorithm),
* and has the following properties: It is an empty tree or its left and right two subtree the absolute value of the difference is not more than 1,
* and about two sub-trees are a balanced binary tree.
* The following code is exactly as defined, first to get the height of the left and right sub-tree (recursive), and then determine whether the left and right subtree is a balanced binary tree,
* Use recursion to complete the decision of the whole tree. Returns true if the condition is fully satisfied.
AC Code:
public class Solution { //Recursive public Boolean isbalanced (TreeNode root) { if (root==null) return true; int leftdepth; int rightdepth; Leftdepth=maxdepth (root.left); Rightdepth=maxdepth (root.right); if (Math.Abs (leftdepth-rightdepth) <=1&&isbalanced (root.left) &&isbalanced (root.right)) return true; else return false; } Recursive code "Maximum Depth of Binary Tree"--https://leetcode.com/problems/maximum-depth-of-binary-tree/public int maxDepth (TreeNode root) { if (root = null) { return 0; } int L = maxDepth (root.left); int r = maxDepth (root.right); return l > R? L + 1:r+ 1; }}
[Leetcode] [Java] Balanced Binary Tree