LeetCode OJ:Balanced Binary Tree
2014-01-22 02:59
267 查看
Balanced Binary Tree
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 binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int dfsdepth(TreeNode *root){ if(root==NULL)return 0; int depl=dfsdepth(root->left); int depr=dfsdepth(root->right); if(fabs(depl-depr)>1)return INT_MAX>>2; return max(depl,depr)+1; } bool isBalanced(TreeNode *root) { if(dfsdepth(root)>=INT_MAX>>2)return false; return true; } };
相关文章推荐
- iis 应用程序池看不到 .net framework 4.0
- 【生物信息学】使用genome 作为ref时,由bam格式 or pileup格式 计算 depth 的 cpp程序
- 基于CAS操作的非阻塞算法
- 多线程的代价及上下文切换
- 死锁,活锁和饥饿
- linux――Mplayer
- Android 键盘事件处理过程
- HMM model 例子 Biased coins
- Java中equals,hashcode
- POJ 3123 Ticket to Ride
- 理解HashMap的原理
- centos minimal 安装无法自定义分区
- [leet code] Length of Last Word
- 一个人的旅行(Dijkstra算法)
- 关于windows7安装python、pip、virtualenv
- winedit 永久试用的办法
- 再开一坑sgu吧
- 学习之路
- 【试着自己写一个STL 】Knife_STL —— config.h
- 【试着自己写一个STL 】Knife_STL —— pair.h