leetcode Balanced Binary Tree 题解
来源:互联网 发布:linux 查看几个cpu 编辑:程序博客网 时间:2024/05/16 06:00
题目描述
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.
思路
判断一棵树是不是平衡二叉树,递归的求每个结点的左子树和右子树的树高,判断左子树和右子树的高度之差不超过1
代码实现
class Solution{public: bool isBalanced(TreeNode *root) { return balancedHeight (root)>=0; } int balancedHeight (TreeNode* root) { if (root==NULL) return 0; int left=balancedHeight (root->left); int right=balancedHeight(root->right); if(left<0 || right<0 ||abs(left - right)>1) return -1; return max(left,right)+1; } };
0 0
- LeetCode题解:Balanced Binary Tree
- LeetCode题解: Balanced Binary Tree
- leetcode Balanced Binary Tree 题解
- LeetCode题解:Balanced Binary Tree
- [LeetCode] Balanced Binary Tree 题解
- LeetCode 题解(20): Balanced Binary Tree
- LeetCode题解——Balanced Binary Tree
- LeetCode题解-110-Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree 题解
- LeetCode 110. 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
- 【leetcode】Balanced Binary Tree
- 企业应用开发面临的问题以及思考
- 大项目源码分析方法总结
- HDU 3912 Turn Right + HDU 1254 推箱子 + HDU 1983 Kaitou Kid - The Phantom Thief (2)
- [网络流24题] 17 运输问题(网络费用流量,最小费用最大流)
- Cordova android框架详解
- leetcode Balanced Binary Tree 题解
- eclipse配置tomcat,以及部署项目到tomcat(解决项目部署后,webapps中无项目文件问题)
- bootstrap css样式起步
- 增加eclipse的内存
- ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法
- Java线程-----------线程安全与不安全与线程同步synchronized和volatile
- Spark进阶视频之模式匹配下的赋值语句
- YUV格式学习:NV16和YUV422P格式互换
- WPF和Windows Phone Post组装函数