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 }
相关文章
- 12-21CF438E The Child and Binary Tree 生成函数、多项式开根
- 12-21CF1528A Parsa‘s Humongous Tree
- 12-21题解 CF504E 【Misha and LCP on Tree】
- 12-21[Codechef - ADITREE] Adi and the Tree - 树链剖分,线段树
- 12-21CodeForces 1092F Tree with Maximum Cost (树形dp)
- 12-21Codeforces Round #358 (Div. 2) Alyona and the Tree
- 12-21Struct_chapter3_Tree
- 12-21Leetcode 1029. Binary Prefix Divisible By 5
- 12-21LeetCode1029. Binary Prefix Divisible By 5(DFA)自动机
- 12-21秦曾昌人工智能课程---6、Decision Tree Learning