Balanced Binary Tree - LeetCode
来源:互联网 发布:蒙大拿级战列舰数据 编辑:程序博客网 时间:2024/05/01 19:33
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
这道题很简单,但是一定要注意随时return!!!代码已AC,如下:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int height(TreeNode root){int h = 1;if(root == null){return h;}else {int i = height(root.left);int j = height(root.right);if(i>j)return (h+i);elsereturn (h+j);} } public boolean isBalanced(TreeNode root) {if(root == null){return true;}else {int i = height(root.left);int j = height(root.right);//System.out.println(i);//System.out.println(j);if(i - j >= 2)return false;else if(j - i >= 2)return false;else{boolean l = isBalanced(root.left);if(l == false)return l;boolean r = isBalanced(root.right);if(r == false)return r;}}return true;}}
0 0
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Leetcode: Balanced Binary Tree
- LeetCode-Balanced Binary Tree
- LeetCode - Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- LeetCode | Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- 腾讯反击:我教阿里做游戏,请阿里教我做公关
- putchar()、getchar()、puts()、gets()
- 如何选择液晶显示器
- VS2010 手动为控件添加事件处理函数
- 深入了解Struts2返回JSON数据的原理及具体应用范例
- Balanced Binary Tree - LeetCode
- 华为专利战升级,以质代量
- java值传递几个注意点
- Outlook2013 新邮件提醒
- 常用正则
- 微信和QQ浏览器绑定(PC端)
- JVM工作原理和特点
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- 利用shared memeory减少对global memory的访问