题目

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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:3/ \9  20/  \15   7
Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:1/ \2   2/ \3   3/ \4   4

解题思路

这一道题是要判断二叉树是否为平衡二叉树,需要递归的判断每个节点是不是平衡的。因为需要查询树的高度,可以直接调用LeetCode: 104. Maximum Depth of Binary Tree的代码。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def isBalanced(self, root: TreeNode) -> bool:if root is None:return Truel = self.maxDepth(root.left)r = self.maxDepth(root.right)print(l,r)if abs(l-r)>1:return Falseelse:return self.isBalanced(root.left) and self.isBalanced(root.right)def maxDepth(self, root: TreeNode) -> int:res = 0if root is None:return reslevel = [root]while level:res += 1internode = []for node in level:internode.extend([node.left, node.right])level = [leaf for leaf in internode if leaf]return res

LeetCode上大佬的解法:

class Solution(object):def isBalanced(self, root):self.isBalanced = Trueself.getHeight(root)return self.isBalanceddef getHeight(self, root):if not root: return 0left = self.getHeight(root.left)right = self.getHeight(root.right)if abs(left - right) > 1: self.isBalanced = Falsereturn max(left, right) + 1

LeetCode: 111. Minimum Depth of Binary Tree相关推荐

  1. leetcode python3 简单题111. Minimum Depth of Binary Tree

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

  2. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. LeetCode 111. Minimum Depth of Binary Tree

    原题 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the s ...

  4. 111. Minimum Depth of Binary Tree

    1.问题描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along t ...

  5. LeetCode算法题-Minimum Depth of Binary Tree(Java实现)

    这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...

  6. leetcode算法题--Minimum Depth of Binary Tree

    原题链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ int minDepth(TreeNode* root) {if (r ...

  7. 111. Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. LeetCode 111. Minimum Depth of Binary Tree--Java, Python解法--二叉树最小高度--迭代,递归

    题目地址:Minimum Depth of Binary Tree - LeetCode Given a binary tree, find its minimum depth. The minimu ...

最新文章

  1. Meta祭出元宇宙「阿拉丁神灯」!LeCun称世界模型将带来像人一样的AI
  2. wireshark 抓包iphone 4g_比4G下续航短2小时,苹果回应iPhone12用5G耗电快:与运营商一道做更多优化工作...
  3. android 渠道号_亲测:安卓打渠道包神器,1分钟出自动出100个渠道包
  4. 荣耀Magic4核心配置曝光:最强驯龙高手 性能远超iPhone 13 Pro
  5. c++学习笔记(5)
  6. Django tips: 查看当前Request所执行的所有SQL
  7. POJ分层练习题目列表
  8. 修改 Ubuntu SSH 登录后的欢迎信息
  9. 创业基础(第7章 创业计划) 来自高校:全国大学生创新创业实践联盟 分类:创新创业 学习规则:按序学习
  10. 小暑海报文案|小暑海报设计图片素材
  11. iOS游戏开始者从零开始 6个月的学习编程经历
  12. C语言编程>第二十七周 ① 请补充fun函数,该函数的功能是:寻找两个整数之间的所有素数(包括这两个整数),把结果保存在数组a中,函数返回素数的个数。
  13. 重写虫虫项目犯的低级错误
  14. video.js 自定义播放组件
  15. 有人爬了一万个购物网站,发现了1818个「年底大促」的商家小伎俩
  16. Java 报错 restartedMain] o.s.boot.SpringApplication : Application run failed
  17. 如何在云服务器上部署web项目(CVM)
  18. #个人日记-电影《哆啦A梦:伴我同行2》观后感-20210530
  19. ooqzxdJeroboam v7.30 Bilingual 1CD瑰就
  20. 数据可视化——词云图

热门文章

  1. 2016年SDN通往成功路的5大步
  2. android脚步---设置layout隐藏属性
  3. tornado缓存技术
  4. Entity Framework Code First添加修改及删除单独实体
  5. 我知道的一些 ”运行“ 窗体下的命令,个人使用
  6. Oracle10g 管理系统全局区(SGA)
  7. WCF系列(四) -- 也谈序列化(上)
  8. 软件测试理论你知道多少?
  9. 涉密计算机格式化维修,涉密计算机中的涉密信息被删除或格式化后,不可复原,可以连接互联网 - 作业在线问答...
  10. numpy之argmax()函数