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.

题目大意:

判断一颗二叉树是否为平衡二叉树。

思路:

  1. 做一个辅助函数来求的树的高度。

  2. 通过辅助函数来递归求解。

代码如下:

/*** 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 平衡二叉树相关推荐

  1. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  2. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

  3. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  4. LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. 110. Balanced Binary Tree 平衡二叉树

    给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,nu ...

  7. [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 ...

  8. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  9. leetcode python3 简单题110. Balanced Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十题 (1)题目 英文: Given a binary tree, dete ...

最新文章

  1. Struts2_day02--封装数据到集合里面
  2. Bzoj4016/洛谷P2993 [FJOI2014] 最短路径树问题(最短路径问题+长链剖分/点分治)
  3. 学电脑从新手到高手_小白如何学手绘插画?新手到高手必学的四套教程【614期】...
  4. how to handdraw a web UI for applications? Figma
  5. socket开发中INADDR_ANY的含义是什么?
  6. A 股暴跌,户均亏 2 万!刚写好的辞职信又撕了……
  7. hduoj 6000 2016CCPC-final B. Wash(贪心)
  8. 手顺解决 Foxmail7.2 无法搜索“主题”和“全文”,只能搜“发件人”和“收件人” 问题
  9. 机器人动力学与控制_快速搭建“机器人动力学-参数辨识-轨迹规划-运动控制”的完整框架...
  10. Hinton、LeCun、Bengio等人获得2022 年阿斯图里亚斯公主技术和科学研究奖!
  11. 桃李春风一杯酒,江湖夜雨十年灯。—第三十五天
  12. diy服务器个人主机_小型企业及个人用户入门级服务器DIY
  13. 网站SEO其实很简单,新手必看!!!
  14. 【JavaSE进阶(上)】自学笔记 记得收藏时时回顾
  15. 深入理解读写锁ReentrantReadWriteLock
  16. 药王修美乐黄金单品战法
  17. [FMG]两个简单的JSP+servlet小程序
  18. 企业内容管理(ECM)与文档管理的区别及其优势
  19. 基于vue的移动端Icon图标拖拽(改变定位和使用transform)
  20. Oracle: SQL精妙SQL语句讲解(常用sql) .

热门文章

  1. UVa1422 Processor
  2. 程序清单4.1_talkback.c程序_《C Primer Plus》P60
  3. 使用meta来刷新网页效果
  4. 在.net中序列化读写xml方法的总结(转载)
  5. input中checked复选框进行单选
  6. iOS音乐后台播放及锁屏信息显示
  7. 路由器LED闪灯泄露数据
  8. koa源码分析-generator和yield分析
  9. MySQL基本配置命令总结
  10. Install And Configure ColdFusion MX 6.1 on Windows