Balanced Binary Tree

Balanced Binary Tree
 1 public class Solution {
 2 public boolean isBalanced(TreeNode root) {
 3 if(root ==null) return true;
 4 if(is(root)==-1) return false;
 5 else return true;
 6 }
 7 public int is(TreeNode root){
 8 if(root==null) return 0;
 9 int left = is(root.left);
10 if(left==-1) return -1;
11 int right = is(root.right);
12 if(right==-1) return -1;
13 if(Math.abs(left-right)>1) return -1;
14 return left>right?left+1:right+1;
15 }
16 }
View Code

Balanced Binary Tree

上一篇:ZT 二叉树先序,中序,后序遍历非递归实现


下一篇:Cassandra1.2文档学习(18)—— CQL数据模型(下)