leetCode 110. Balanced Binary Tree 平衡二叉树
110. 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 a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int depth(TreeNode* root){if(!root)return 0;int l = depth(root->left) ;int r = depth(root->right) ;return 1 + ((l > r)?l:r);}bool isBalanced(TreeNode* root) {if(!root)return true;else{int l = depth(root->left);int r = depth(root->right);if(l + 1 < r || r + 1 <l){return false;}elsereturn (isBalanced(root->left) && isBalanced(root->right) );}}
};
2016-08-08 00:25:26
转载于:https://blog.51cto.com/qiaopeng688/1835477
leetCode 110. Balanced Binary Tree 平衡二叉树相关推荐
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
- LeetCode 110 Balanced Binary Tree
LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode python3 简单题110. Balanced Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...
最新文章
- Struts2_day02--封装数据到集合里面
- Bzoj4016/洛谷P2993 [FJOI2014] 最短路径树问题(最短路径问题+长链剖分/点分治)
- 学电脑从新手到高手_小白如何学手绘插画?新手到高手必学的四套教程【614期】...
- how to handdraw a web UI for applications? Figma
- socket开发中INADDR_ANY的含义是什么?
- A 股暴跌,户均亏 2 万!刚写好的辞职信又撕了……
- hduoj 6000 2016CCPC-final B. Wash(贪心)
- 手顺解决 Foxmail7.2 无法搜索“主题”和“全文”,只能搜“发件人”和“收件人” 问题
- 机器人动力学与控制_快速搭建“机器人动力学-参数辨识-轨迹规划-运动控制”的完整框架...
- Hinton、LeCun、Bengio等人获得2022 年阿斯图里亚斯公主技术和科学研究奖!
- 桃李春风一杯酒,江湖夜雨十年灯。—第三十五天
- diy服务器个人主机_小型企业及个人用户入门级服务器DIY
- 网站SEO其实很简单,新手必看!!!
- 【JavaSE进阶(上)】自学笔记 记得收藏时时回顾
- 深入理解读写锁ReentrantReadWriteLock
- 药王修美乐黄金单品战法
- [FMG]两个简单的JSP+servlet小程序
- 企业内容管理(ECM)与文档管理的区别及其优势
- 基于vue的移动端Icon图标拖拽(改变定位和使用transform)
- Oracle: SQL精妙SQL语句讲解(常用sql) .