剑指offer 面试题39:判断平衡二叉树

提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192

时间限制:1秒       空间限制:32768K      参与人数:2481

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
分析:
平衡二叉树定义


递归解法 AC代码:
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int getHeight(TreeNode *root)
{
if(root==NULL) return 0;
int lh=getHeight(root->left);
int rh=getHeight(root->right);
int height=(lh<rh)?(rh+1):(lh+1); // 记住:要加上根节点那一层,高度+1
return height;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL) return true;
int lh=getHeight(pRoot->left);
int rh=getHeight(pRoot->right);
if(lh-rh>1 || lh-rh<-1) return false;
else return (IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right)); // 继续递归判断
}
};
// 以下为测试
int main()
{
Solution sol;
TreeNode *root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
bool res=sol.IsBalanced_Solution(root);
cout<<res<<" ";
return 0;
}

LeetCode 110. Balanced Binary Tree

Total Accepted: 111269 Total Submissions: 326144 Difficulty: Easy


提交网址: https://leetcode.com/problems/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.

AC代码:

class Solution {
public:
int getHeight(TreeNode *root)
{
if(root==NULL) return 0;
int lh=getHeight(root->left);
int rh=getHeight(root->right);
int height=(lh<rh)?(rh+1):(lh+1); // 记住:要加上根节点那一层,高度+1
return height;
}
bool isBalanced(TreeNode *root) {
if(root==NULL) return true;
int lh=getHeight(root->left);
int rh=getHeight(root->right);
if(lh-rh>1 || lh-rh<-1) return false;
else return (isBalanced(root->left) && isBalanced(root->right)); // 继续递归判断
}
};

C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解的更多相关文章

  1. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  2. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

  3. C++版 - 剑指offer 面试题31:连续子数组的最大和 题解

    剑指offer:连续子数组的最大和 提交网址: http://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&am ...

  4. C++版 - 剑指offer 面试题7:用两个栈实现队列 题解

    用两个栈实现队列 提交网址:  http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=1 ...

  5. C++版 - 剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题,ZOJ 1088:System Overload类似)题解

    剑指Offer 面试题45:圆圈中最后剩下的数字(约瑟夫环问题) 原书题目:0, 1, - , n-1 这n个数字排成一个圈圈,从数字0开始每次从圆圏里删除第m个数字.求出这个圈圈里剩下的最后一个数字 ...

  6. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  7. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  8. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  9. C++版 - 剑指offer 面试题22:栈的压入、弹出序列 题解

    剑指offer 面试题22:栈的压入.弹出序列 提交网址: http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId ...

随机推荐

  1. 微服务框架——SpringCloud(二)

    1.Feign声明式服务调用(负载均衡+熔断器) a.概念:Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign整合了Ribbon和Hys ...

  2. python搭建opencv

    说明 windows下:以管理员身份使用cmd或者powershell. 安装 依次输入以下命令,安装numpy, Matplotlib, opencv三个包 pip install --upgrad ...

  3. Win Server 2003 10条小技巧

    微软推出Windows Server 2003已经有一段时间了,但是,由于它是一个面向企业用户的服务器操作系统,所以,没有引起更多个人用户的注意.实际上,简单地改变一下系统的设置,您也可以将Windo ...

  4. NOI2017总结

    时光剥离你我像一袭华美衣衫 却要被追悔爬满 退役之战,去得匆匆,看得蒙蒙. 第三次全国赛,曾经的APIO初二选手也走到了时间的尽头. 第一次走向全国舞台的激动与忐忑,第一次在大赛中失利的沮丧与绝望,第 ...

  5. 心得体会,搞清楚你为什么学习C++?

    小编作为一名初学者时,从来没问过自己学习C语言.C++等语言是为了什么? 一开始,接触到这个行业可以说是有种魔力引导我,感到了很大的兴趣,很有意思. 我试着读资料,报名学习,找资料,可算是功夫不负有心 ...

  6. windows10计划任务启动bat执行jar打包的jar文件

    今天公司要用bat文件执行jar打包的java文件,运行没项目的程序,并且用任务计划开机自启,今天记录下坑 系统:win10 一.编写.bat执行用jar打包的jar文件 @echo off java ...

  7. 解决iPhone Safari 兼容性CSS背景显示不全问题

    https://jingyan.baidu.com/article/ca2d939d014ccbeb6c31ceb7.html 看到了这个文章解决的.中心部分小于980的时候回出现.苹果手机中的saf ...

  8. [LeetCode] Car Fleet 车队

    N cars are going to the same destination along a one lane road.  The destination is target miles awa ...

  9. C++实验一

    实验结论 2-28 if...else #include <iostream> #include <stdlib.h> using namespace std; int mai ...

  10. Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

    A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catalina.Li ...