Balanced Binary Tree
来源:互联网 发布:matlab字符串数组赋值 编辑:程序博客网 时间:2024/05/21 07:55
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 ofevery node never differ by more than 1.
题目解析:
判断一棵树是不是平衡二叉数
1、他的左子树是平衡二叉树
2、他的右子树是平衡二叉树
3、他的左右子树的深度之差小于等于1
#include<iostream>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};int depthTree(TreeNode *root){if(root == NULL)return 0;return max(depthTree(root->left) + 1,depthTree(root->right) + 1);}bool isBalanced(TreeNode *root) {if(root == NULL)return true;if(isBalanced(root->left) && isBalanced(root->right) && abs( depthTree(root->left)-depthTree(root->right) )<=1 ){return true;}else{return false;}}int main(void){system("pause");return 0;}
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
- interleave词组
- sgu153:Playing with matches
- python从apk文件中获取app的名字
- 极路由2-极贰9007的降级root攻略
- C语言中union与大小端模式的理解
- Balanced Binary Tree
- json预习与自学
- javascript简易日历
- [SDOI2009]晨跑
- Installing VMware Tools in a Linux virtual machine using a Compiler (1018414)
- 跟我一起写makefile二
- 跟我一起写makefile三
- 十一月总结
- 跟我一起写makefile四