15算法课程 110. Balanced Binary Tree
来源:互联网 发布:淘宝买家怎么快速升钻 编辑:程序博客网 时间:2024/05/26 02:53
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.
solution:使用一个计算深度的函数来做辅助
code:
class Solution {public:int cntHeight(TreeNode *root) { if(root == NULL) return 0; int l = cntHeight(root->left); int r = cntHeight(root->right); if(l < 0 || r < 0 || abs(l-r) > 1) return -1; //自定义 return -1,表示不平衡的情况 else return max(l, r) + 1;}bool isBalanced(TreeNode *root) { if(root == NULL) return true; int l = cntHeight(root->left); int r = cntHeight(root->right); if(l < 0 || r < 0 || abs(l-r) > 1) return false; else return true;}};
阅读全文
0 0
- 15算法课程 110. Balanced Binary Tree
- 110.Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 110. Balanced Binary Tree
- 事务以及JDBC操作事务的学习
- lxml库和贴吧图片下载案例
- 1701H5 范江睿 10月20日 连续第10天总结
- Infinite monkey theorem
- 【Python】使用openCV与dlib实现人脸68特征点的检测与手动修改
- 15算法课程 110. Balanced Binary Tree
- 解决virtualBox虚拟机不可使用问题
- [NOIP2017模拟]裁剪表格
- [树链剖分] [bzoj2243] [SDOI2011]染色
- 浏览器渲染机制之DOM树
- java实现map和object互转的三种方法
- [noip2016]组合数问题 题解
- Linux装机必备(centos)
- C++笔记——公有继承、私有继承、保护继承、多重继承