【LEETCODE】110-Balanced Binary Tree
来源:互联网 发布:js检测ie浏览器版本 编辑:程序博客网 时间:2024/05/16 23:33
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 ofevery node never differ by more than 1.
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def getDepth(self,root): #先写一个获得深度的函数 #depth = 0 if root is None: return 0 else: #depth +=1 return max(self.getDepth(root.left),self.getDepth(root.right))+1 def isBalanced(self, root): """ :type root: TreeNode :rtype: bool """ if root is None: return True elif abs(self.getDepth(root.left)-self.getDepth(root.right))<=1: #当前node的左子树的depth与右子树的depth之差<=1时 return self.isBalanced(root.left) and self.isBalanced(root.right) #则递归继续判断左右子树 else: return False
0 0
- LeetCode(110)Balanced Binary Tree
- LeetCode 110 Balanced Binary Tree
- LeetCode: Balanced Binary Tree [110]
- LeetCode 110 Balanced Binary Tree
- [leetcode 110] Balanced Binary Tree
- leetcode-110 Balanced Binary Tree
- leetCode #110 Balanced Binary Tree
- [LeetCode 110]Balanced Binary Tree
- leetcode || 110、Balanced Binary Tree
- Leetcode[110]-Balanced Binary Tree
- LeetCode 110 Balanced Binary Tree
- leetcode[110]:Balanced Binary Tree
- LeetCode(110) Balanced Binary Tree
- leetcode 110Balanced Binary Tree
- Leetcode# 110 Balanced Binary Tree
- leetcode 110: Balanced Binary Tree
- leetcode-110-Balanced Binary Tree
- LeetCode 110 : Balanced Binary Tree
- C++基础——用C++实例理解UML类图
- 使用gradle编译和运行Java程序
- Mac 自带shell 与 iTerm2 快捷键
- 无锁编程
- PHP微信开发---查询微信精选文章
- 【LEETCODE】110-Balanced Binary Tree
- VisualStudio报错This function or variable may be unsafe的解决方法
- android 音频播放 以及 二维码模块中音频播放和震动(vibrate)的实现
- 安卓问题整理
- 线程分析.....
- 一个iOS9 Bug:弄死iPhone很容易
- 出现junit 出错,找不到文件,可以这么解决。
- 两片74ls138组成4/16译码器-20151030
- NSApp openURL 坑人