Balanced Binary Tree
来源:互联网 发布:2016无限申请淘宝小号 编辑:程序博客网 时间:2024/06/06 01:22
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.
#include<iostream>#include<vector>#include<algorithm>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};bool FLAG = true;int MaxDepth(TreeNode* root ){if (root == NULL||FLAG==false)return 0;int leftdepth = MaxDepth(root->left);int rigtdepth = MaxDepth(root->right);if (abs(leftdepth - rigtdepth) > 1){FLAG = false;return -1;}else if (leftdepth < rigtdepth)return rigtdepth + 1;elsereturn leftdepth + 1;}bool isBalanced(TreeNode *root) {MaxDepth(root);return FLAG;}
0 0
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Zookeeper 初体验之——JAVA实例
- c++模板 template的使用
- hdu1151 二分图的最小路径覆盖
- 有关ImageView的圆角或弧度角设置
- More is better
- Balanced Binary Tree
- 各种距离汇总
- ObjC的initialize和init
- 蓝桥杯 算法训练 出现次数最多的整数
- URAL 1385 Interesting Number
- JAVA 枚举类型
- C++中new和delete用法
- RatingBar简单介绍
- iOS新浪微博客户端开发(4)——自定义微博Cell的实现