【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 ...
随机推荐
- Web网页的了解内容
一.因特网(互联网) 1.发展阶段: A 实验科研阶段 B社会化应用启动阶段 C社会化应用阶段 2.基本特征:资源共享 二.万维网 1.全称全球信息网(World Wide web),简称Web 2. ...
- WPFの静态资源(StaticResource)和动态资源(DynamicResource)
下面是前台代码: <Window.Resources> <TextBlock x:Key="res1" Text="好好学习"/ ...
- 第四章 函数(JavaScript:语言精粹)
函数包含一组语句,用来指定对象的行为,其代码可以用来重复使用. 一般来说,编程就是将一组需求分解成一组函数和数据结构的技能. 概览:函数对象 | 函数字面量 | 调用 | 方法调用模式 | 函 ...
- 批量运行R包
#批量运行包:all.pcg <- c("data.table","ggplot2","rmarkdown","tidyr& ...
- php中的json_encode()和json_decode()函数的一些说明
一,json语法( php中的json_decode($json)中的$json要符合json语法格式 ) ① JSON可以表示三种类型的值 1,简单值.包括整型,字符串型,布尔值和null.例如:5 ...
- Model Binding
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- CSS 超出隐藏问题
.dropdown-navbar>li:last-child>a { border-bottom: 0 solid #DDD; border-top: 1px dotted transpa ...
- linux进程编程:子进程创建及执行函数简介
linux进程编程:子进程创建及执行函数简介 子进程创建及执行函数有三个: (1)fork();(2)exec();(3)system(); 下面分别做详细介绍.(1)fork() 函数定 ...
- JAVA利用JXL导出/生成 EXCEL
/** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...
- Spring—Quartz定时调度CronTrigger时间配置格式说明与实例
1 .CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 ...