【LeetCode OJ】Balanced Binary Tree
Problem Link:
http://oj.leetcode.com/problems/balanced-binary-tree/
We use a recursive auxilar function to determine whether a sub-tree is balanced, if the tree is balanced, it also return the depth of the sub-tree.
A tree T is balanced if the following recursive conditions are satisfied:
- T's left sub-tree is balanced;
- T's right sub-tree is balanced;
- The depth of the two sub-trees never differe by more than 1.
The python code is as follows, where a recursive function check the balance in the way of top-down.
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return a boolean
def isBalanced(self, root):
return self.balanced_and_depth(root)[0] def balanced_and_depth(self, node):
"""
Return [False, 0] if the sub-tree of node is not balanced,
return [True, depth] otherwise
"""
if not node:
return True, 0
lb, ld = self.balanced_and_depth(node.left)
if not lb:
return False, 0
rb, rd = self.balanced_and_depth(node.right)
if not rb:
return False, 0
if abs(ld-rd) > 1:
return False, 0
return True, 1 + max(ld,rd)
【LeetCode OJ】Balanced Binary Tree的更多相关文章
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 【LeetCode OJ】Flatten Binary Tree to Linked List
Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...
- 【LeetCode OJ】Validate Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...
- 【LeetCode OJ】Recover Binary Search Tree
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【leetcode❤python】107. Binary Tree Level Order Traversal II
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】102. Binary Tree Level Order Traversal
#-*- coding: UTF-8 -*-#广度优先遍历# Definition for a binary tree node.# class TreeNode(object):# def ...
- LeetCode OJ 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- iOS,Xcode7 制作Framework,含资源和界面
Xcode7 制作Framework 本文通过Demo方式介绍1)将含bundle和存代码编写界面打包进framework:2)将storyboard +assets.xcassets打包. (一) ...
- C++库汇总
C++库汇总 C++类库介绍再次体现了C++保持核心语言的效率同时大力发展应用库的发展趋势!!在C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功 ...
- DSO、CUBE区别(覆盖、合计)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- XMLHttpRequest 加载进度
XMLHttpRequest 相关资料请移步这里直接查看,我这里就不在赘述: https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpReque ...
- 封装对MongoDB数据库的增删改查访问方法(基于MongoDB官方发布的C#驱动)
本文利用MongoDB官方发布的C#驱动,封装了对MongoDB数据库的增删改查访问方法.先用官方提供的mongo-csharp-driver ,当前版本为1.7.0.4714 编写数据库访问帮助类 ...
- ural1057 Amount of Degrees
链接 这题有一点小坑点 就是AX^B A只能为0或者1 ,剩下的就比较好做的了. #include <iostream> #include<cstdio> #include ...
- Android Tips – 填坑手册
出于: androidChina http://www.androidchina.net/3595.html 学习 Android 至今,大大小小的坑没少踩,庆幸的是,在强大的搜索引擎与无私奉献的 ...
- The import javax.servlet.http.HttpServletRequest cannot be resolved
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...
- 0060 Linux SELinux 管理命令
1. SELinux 的起源 SELinux 是一个面向政府和行业的产品,由 NSA.Network Associates.Tresys 以及其他组织设计和开发.尽管 NSA 将其作为一个补丁集引入, ...