- //用递归的思想,先判断总体是否平衡,再递归判断左子树和右子树
- public class Solution {
- public boolean IsBalanced_Solution(TreeNode root) {
- if(root==null)
- return true;
- int left=depth(root.left);
- int right=depth(root.right);
- if(Math.abs(left-right)>1)
- return false;
-
- boolean booleft=IsBalanced_Solution(root.left);
- boolean booright=IsBalanced_Solution(root.right);
- return booleft&&booright;
- }
-
- public int depth(TreeNode root){
- if(root==null)
- return 0;
- int left=depth(root.left);
- int right=depth(root.right);
- return (left>right)?(left+1):(right+1);
- }
- }
posted @
2018-03-14 17:34
零。幺
阅读(
262)
评论()
编辑
收藏
举报