当前位置: 首页 > news >正文

LeetCode: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.

分析:判断一颗二叉树是否是平衡的,dfs递归求解,递归的过程中顺便求得树的高度                                       本文地址

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  * int val;
 5  * TreeNode *left;
 6  * TreeNode *right;
 7  * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isBalanced(TreeNode *root) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         if(root == NULL)return true;
16         if(height(root) == -1)return false;
17         else return true;
18     }
19     //若root是平衡树,那么返回树的高度,否则返回-1
20     int height(TreeNode *root)
21     {
22         if(root->left == NULL && root->right == NULL)return 1;
23         int leftHeight = 0, rightHeight = 0;
24         if(root->left)
25             leftHeight = height(root->left);
26         if(leftHeight == -1)return -1;
27         if(root->right)
28             rightHeight = height(root->right);
29         if(rightHeight == -1)return -1;
30         if(abs(leftHeight-rightHeight) > 1)return -1;
31         return 1+max(leftHeight, rightHeight);
32     }
33 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440069.html

转载于:https://www.cnblogs.com/TenosDoIt/p/3440069.html

相关文章:

  • 看博客学学Android(二十二)
  • Jquery重新学习之一[加载与属性html(),text(),val()]
  • Ubuntu13.10安装仿苹果启动菜单Cairo-Dock
  • Linux awk 命令 说明
  • 数组资源
  • html取出指定div的内容(不怕嵌套)
  • (转)Linux整合apache和tomcat构建Web服务器
  • 同一台Windows机器中启动多个Memcached服务
  • WebSphere MQ 入门指南
  • Glusterfs3.3.1DHT(hash分布)源代码分析
  • 进入保护模式(一)
  • SmartWatch2开发-ControlSample分析
  • 回车和换行
  • [Jquery] 实现鼠标移到某个对象,在旁边显示层。
  • 【转】Navigation Drawer(导航抽屉)
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 10个确保微服务与容器安全的最佳实践
  • Median of Two Sorted Arrays
  • mysql_config not found
  • PermissionScope Swift4 兼容问题
  • Redux系列x:源码分析
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • Vue ES6 Jade Scss Webpack Gulp
  • vue.js框架原理浅析
  • vue:响应原理
  • 测试开发系类之接口自动化测试
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 聊聊flink的BlobWriter
  • 浏览器缓存机制分析
  • 前嗅ForeSpider中数据浏览界面介绍
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 通过git安装npm私有模块
  • 一个项目push到多个远程Git仓库
  • 异步
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • 你对linux中grep命令知道多少?
  • 阿里云ACE认证学习知识点梳理
  • #include
  • #stm32驱动外设模块总结w5500模块
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • (1)SpringCloud 整合Python
  • (3)llvm ir转换过程
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (八十八)VFL语言初步 - 实现布局
  • (二)hibernate配置管理
  • (二)windows配置JDK环境
  • (翻译)terry crowley: 写给程序员
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (四)库存超卖案例实战——优化redis分布式锁
  • (万字长文)Spring的核心知识尽揽其中
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • .NET 设计模式初探