python二叉树深度 判断平衡树
来源:互联网 发布:国际航协2015数据 编辑:程序博客网 时间:2024/06/01 10:32
#-*- coding:utf-8 -*-class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Noneclass Solution: def getDepth(self , Root): if Root == None: return 0; lDepth = self.getDepth(Root.left); rDepth = self.getDepth(Root.right); return max(lDepth , rDepth) + 1; def IsBalanced(self, pRoot): if not pRoot: return True lDepth = self.getDepth(pRoot.left); rDepth = self.getDepth(pRoot.right); diff = lDepth - rDepth; if diff < -1 or diff > 1: return False; return self.IsBalanced(pRoot.left) and self.IsBalanced(pRoot.right);tn= TreeNode(1)ltn=TreeNode(2)rtn=TreeNode(3)tn.left=ltnltn.right=rtnprint( Solution().IsBalanced(tn))
阅读全文
0 0
- python二叉树深度 判断平衡树
- 二叉树深度、平衡二叉树的判断
- 【笔试】50、二叉树的深度&&判断平衡二叉树
- [剑指offer]二叉树深度vs判断平衡二叉树
- 二叉树的深度以及判断平衡二叉树
- 二叉树深度和判断平衡二叉树
- 二叉树的深度,判断是否是平衡二叉树
- 二叉树的深度以及判断平衡二叉树
- 求二叉树深度、判断是否是平衡二叉树
- 39平衡二叉树判断python
- 判断平衡二叉树
- 判断二叉树平衡
- 平衡二叉树判断
- 判断平衡二叉树
- 判断平衡二叉树
- 平衡二叉树判断
- 判断平衡二叉树
- 判断平衡二叉树
- Win7SP1 64位原版系统安装 VS2013 with update 5 小记
- Scala---Akka Actor(三)Actor的持久化
- docker 时区问题
- Anagrams 归类 的 python 版本代码
- 图算法:Bellman-Ford算法和SPFA优化
- python二叉树深度 判断平衡树
- 《leetcode》remove-duplicates-from-sorted-array-ii
- 安装Xcode遇到的问题
- Ipython常用命令
- 配置Hercules的TCP/IP
- 小白入门---HTML5音乐播放器
- JavaScript-2-5:scroll、client、阻止冒泡
- 解决了电脑经常死机的问题
- 2012ICPC长春站 A Browsing History 【字符串】