[LeetCode]Balanced Binary Tree
2015-12-05 13:02
363 查看
题目描述:(链接)
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.
解题思路:
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) { return height(root) >= 0; } int height(TreeNode *root) { if (root == nullptr) return 0; int left = height(root->left); int right = height(root->right); if (left < 0 || right < 0 || abs(left - right) > 1) { return -1; } return max(left, right) + 1; } };
相关文章推荐
- Single Number
- hpu(acm)大赛第一题
- 最大上升子序列和
- AR路由综合
- 扣丁学堂——Activity(二)
- 查看Casio PDA 的物理地址 MAC (DT-X7系列)
- C#中NPOI操作excel之读取和写入excel数据
- Unity+Android交互教程——让手机"动"起来
- OpenCV入门:(五:更改图片对比度和亮度)
- HFile Mapreduce阶段的优化
- 解决yum安装软件报Couldn't resolve host 'mirrorlist.centos.org问题
- MFC技术积累——基于MFC对话框类的那些事儿3
- linux 程序调试
- Android群英传-拼图游戏puzzle-6点吐槽
- Android群英传-拼图游戏puzzle-6点吐槽
- Android群英传-拼图游戏puzzle-6点吐槽
- LintCode-剑指Offer-(112)删除排序链表中的重复元素
- 优秀程序员的七大特征,你具备几条?
- hdoj5024Wang Xifeng's Little Plot【dfs】
- c# 和 sqlserver 中的事务