110. 平衡二叉树

题目链接:

https://leetcode-cn.com/problems/balanced-binary-tree/

解题思路:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public boolean isBalanced(TreeNode root) {
12         if(root==null)
13             return true;
14         if(Math.abs(maxDepth(root.left)-maxDepth(root.right))>1)
15             return false;
16         else
17         {
18             if(isBalanced(root.right)&&isBalanced(root.left))
19                 return true;
20             else
21                 return false;
22         }
23     }
24     public int maxDepth(TreeNode root)//计算树层高
25     {
26         if(root==null)
27             return 0;
28         return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
29     }
30 }
原文地址:https://www.cnblogs.com/wangyufeiaichiyu/p/11249035.html