LeetCode_Balanced Binary Tree
2015-09-04 19:23
295 查看
原题链接:
class Solution {
public:
bool isBalanced(TreeNode* root) {
bool flag = true;
if(!root) //空树
return true;
start(root,flag);
return flag;
}
void start(TreeNode* root,bool &flag)
{
if(!root)
{
return;
}
int leftPath = 0,rightPath = 0;
leftPath = searchPath(root->left);//找左子树的层数
rightPath = searchPath(root->right);//找右子树的层数
if(abs(leftPath-rightPath) > 1)
{
flag = false;
return;
}
if(root->left)
start(root->left,flag);
if(root->right)
start(root->right,flag);
}
int searchPath(TreeNode *root)
{
if(root == NULL)
return 0;
int ldepth = searchPath(root->left);
int rdepth = searchPath(root->right);
return ldepth > rdepth ? ldepth+1:rdepth+1;
}
class Solution {
public:
bool isBalanced(TreeNode* root) {
bool flag = true;
if(!root) //空树
return true;
start(root,flag);
return flag;
}
void start(TreeNode* root,bool &flag)
{
if(!root)
{
return;
}
int leftPath = 0,rightPath = 0;
leftPath = searchPath(root->left);//找左子树的层数
rightPath = searchPath(root->right);//找右子树的层数
if(abs(leftPath-rightPath) > 1)
{
flag = false;
return;
}
if(root->left)
start(root->left,flag);
if(root->right)
start(root->right,flag);
}
int searchPath(TreeNode *root)
{
if(root == NULL)
return 0;
int ldepth = searchPath(root->left);
int rdepth = searchPath(root->right);
return ldepth > rdepth ? ldepth+1:rdepth+1;
}
相关文章推荐
- Apache Camel的split使用示例
- javaSE基础编程——排序(冒泡,选择)
- linux文件系统管理一创建文件
- HDU 3555 Bomb
- poj 3579 Median (二分搜索之查找第k大的值)
- Pop–实现任意iOS对象的任意属性的动态变化
- XML解析之SAX解析
- 可能与深度学习跨媒体相关的几篇文章
- javaSE基础编程——输出10000到100000之间的回文数
- HTTP状态码详解
- android studio开发Activity切换动画以及ButterKnife注解简单应用
- 数据结构学习之直接插入排序
- hdu-3436 Queue-jumpers(伸展树)
- 通过深度图实时鲁棒地识别手势 - Realtime and Robust Hand Tracking from Depth
- LeetCode -- Count Complete Tree Node
- STM32利用FATFS读写数组
- lodash 中文学习拾零之 Collection篇
- 学前班 第3节 怎么看原理图之内存类接口
- noip2005 陶陶摘苹果 (模拟)
- unity 3d学习第三天