leetcode第一刷_Balanced Binary Tree
来源:互联网 发布:jquery ajax传递json 编辑:程序博客网 时间:2024/05/20 01:08
二叉平衡树好火啊,几乎每个公司的笔试题里都有它,考了好多次我都不会,挂笔试很有可能就是因为它,还有一个它的同伙叫二叉搜索树,貌似人气比它还要高一些。二叉平衡树是什么样的树呢,是每个节点的左右子树高度相差绝对值都不超过1。好,你说你终于回了,这不很简单吗,求一下根节点的左右字数高度,如果满足,他就是,否则就不是嘛。不是啊亲,要求是所有节点都满足这个条件,判断的时候必须每个节点都验证的!
扯了这么长,其实看看代码就明白了,怎么有种在贴吧发言要凑够15字的感觉。
- int getHeight(TreeNode *root){
- if(root == NULL) return 0;
- if(!root->left&&!root->right) return 1;
- return max(getHeight(root->left), getHeight(root->right))+1;
- }
- class Solution {
- public:
- bool isBalanced(TreeNode *root) {
- if(root == NULL) return true;
- if(!root->left && !root->right) return true;
- if(isBalanced(root->left)&&isBalanced(root->right)&&abs(getHeight(root->left)-getHeight(root->right))<=1)
- return true;
- return false;
- }
- }; http://pro851ecb33.isitestar.cn/
http://pro3e101ba8.isitestar.cn/
0 0
- leetcode第一刷_Balanced Binary Tree
- leetcode第一刷_Balanced Binary Tree
- leetcode第一刷_Recover Binary Search Tree
- leetcode第一刷_Validate Binary Search Tree
- leetcode第一刷_ Flatten Binary Tree to Linked List
- leetcode第一刷_Minimum Depth of Binary Tree
- leetcode第一刷_Convert Sorted List to Binary Search Tree
- leetcode第一刷_Convert Sorted Array to Binary Search Tree
- leetcode第一刷_Maximum Depth of Binary Tree
- leetcode第一刷_Add Binary
- leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
- leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
- leetcode第一刷_Symmetric Tree
- leetcode第一刷_Same Tree
- [leetcode刷题系列]Balanced Binary Tree
- LeetCode刷题笔录Balanced Binary Tree
- 开始刷leetcode day63:Invert Binary Tree
- Leetcode - Tree - Binary Tree Traversal
- ffmpeg快速命令使用
- java中栈和堆内存的区别
- 高通物联网技术解决方案:从汽车到智慧城市
- hdu 1867 求两个串的"和"最小 ,KMP
- mySql数据库
- leetcode第一刷_Balanced Binary Tree
- JDBC连接Mysql并统计指定关键词在某一列中出现的次数
- ubuntu系统下如何禁用笔记本触摸板。
- sphinx 配置文件全解析
- asp.net mvc3 数据验证(二)——错误信息的自定义及其本地化
- python 笔记---set
- asp.net mvc3 数据验证(三)—自定义数据注解
- QT内label控件通过opencv显示图像
- django models choices添加选项