https://leetcode.com/problems/balanced-binary-tree/
Https://leetcode.com/discuss/28162/java-o-n-solution-based-on-maximum-depth-of-binary-tree
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.
Public classSolution {Private Booleanresult =true; Public Booleanisbalanced (TreeNode root) {maxDepth (root); returnresult;} Public intmaxDepth (TreeNode root) {if(Root = =NULL) return0; intL =maxDepth (Root.left); intR =maxDepth (root.right); if(Math.Abs (L-r) > 1) Result=false; return1 +Math.max (L, R);}}
1 Public classSolution {2 Public Booleanisbalanced (TreeNode root) {3 if(Root = =NULL)4 return true;5 if(Math.Abs (height (root.left)-height (root.right)) <= 1)6 return(isbalanced (Root.left) &&isbalanced (root.right));7 return false;8 }9 Public intheight (TreeNode root) {Ten if(Root = =NULL) One return0; A intleft =height (root.left); - intright=height (root.right); - return(Math.max (left,right) +1); the - } -}
Balanced Binary Tree