110. Balanced Binary Tree
来源:互联网 发布:js racing 编辑:程序博客网 时间:2024/06/06 19:56
/*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.*//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isBalanced(TreeNode* root) { if(!root) return true; return depth(root)!=-1; } int depth(TreeNode* root) { if(!root) return 0; int leftdepth=depth(root->left); if(leftdepth==-1) return -1; int rightdepth=depth(root->right); if(rightdepth==-1) return -1; if(abs(leftdepth-rightdepth)>1) return -1; return max(leftdepth,rightdepth)+1; }};
阅读全文
0 0
- 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
- caffe的matlab接口,caffe.Net()崩溃
- 分页点击下一页查询时 失去条件
- 谈谈encodeURI和encodeURIComponent以及escape的区别与应用
- WeakHashMap存在的原因
- Kotlin基础(一)android studio中配置Kotlin
- 110. Balanced Binary Tree
- T-SQL查询进阶--理解SQL SERVER中的分区表
- 运算符操作
- ffmpeg源码简析 结构总览
- html5使用<marquee>标签实现走马灯效果
- 基于java config的springSecurity(四)--启用全局方法安全
- JAVA编程中性能优化的一些地方
- 浅谈黑盒测试和白盒测试
- Java并发编程之Lock