【leetcode】Balanced Binary Tree(middle)
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.
思路:
我居然在这道题上卡了一个小时。关键是对于平衡的定义,我开始理解错了,我以为是要所有叶节点的高度差不大于1.
但题目中的定义是如下这样的:
Below is a representation of the tree input: {1,2,2,3,3,3,3,4,4,4,4,4,4,#,#,5,5}
:
____1____
/ \
2 2
/ \ / \
3 3 3 3
/\ /\ /\
4 4 4 4 4 4
/\
5 5
Let's start with the root node (1). As you can see, left subtree's depth is 5
, while right subtree's depth is 4
. Therefore, the condition for a height-balanced binary tree holds for the root node. We continue the same comparison recursively for both left and right subtree, and we conclude that this is indeed a balanced binary tree.
我AC的代码:我觉得我的代码就挺好挺短的。
bool isBalanced3(TreeNode* root){
int depth = ;
return isBalancedDepth(root, depth);
} bool isBalancedDepth(TreeNode* root, int &depth)
{
if(root == NULL) return true;
int depthl = , depthr = ;
bool ans = isBalancedDepth(root->left, depthl) && isBalancedDepth(root->right, depthr) && abs(depthl - depthr) < ;
depth = ((depthl > depthr) ? depthl : depthr) + ;
return ans;
}
其他人的代码,对高度多遍历了一遍,会比较慢:
bool isBalanced(TreeNode *root) {
if (!root) return true;
if (abs(depth(root->left) - depth(root->right)) > ) return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int depth(TreeNode *node){
if (!node) return ;
return max(depth(node->left) + , depth(node->right) + );
}
【leetcode】Balanced Binary Tree(middle)的更多相关文章
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- 【leetcode】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a bina ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
随机推荐
- Linux下运行C语言程序
一.编写C语言的源代码 二.用gcc -c C文件名生成.o文件 三.用gcc -o 可执行文件名 .o文件名 生成可执行文件 四.输入可执行文件名前加./执行可执行文件
- httpd服务访问控制
客户机地址限制 通过配置Order.Deny from.Allow from 来限制客户机 allow.deny :先"允许"后"拒绝" ,默认拒绝所有为明确的 ...
- DAY2 Python 标准库 -> Getpass 模块 -> 命令行下输入密码的方法.
getpass 模块 getpass 模块提供了平台无关的在命令行下输入密码的方法. getpass(prompt) 会显示提示字符串, 关闭键盘的屏幕反馈, 然后读取密码. 如果提示参数省略, 那么 ...
- Java Io 之 编码
Java字符串编码一些知识总结: package com.dcz.io; import java.io.UnsupportedEncodingException; public class Encod ...
- PowerDesigner的使用(一)
一. PowerDesigner 功能 1. 需求管理:记录需求,分析设计模型 2. 生成文档:生成HTML格式文档,方便沟通. 3. 影响度分析:模型之间连接起来,同步修改功能. 4. 数据映射:提 ...
- 如何使用java自定义注解?demo
1.Description.java package kzfy.bk.com; import java.lang.annotation.Documented; import java.lang.ann ...
- BZOJ2809——[Apio2012]dispatching
1.题目大意:给一棵树和M值,每个点有两个权值C和L,选x个点,这x个点的C值的和不能超过M,且这x个点如果都在某个子树内 定义满意度为x*这个子树的根的L值 2.分析:这是一道可并堆的题目,我们考虑 ...
- Fedora 24最新工作站版本之四大重要改进
导读 2014年,Fedora.next倡议正式开始建立Fedora Linux未来十年的发展规划.从本质上讲,这项规划旨在进一步使Fedora不再只是一套汇聚多种开源产品的通用库(例如Debian) ...
- 迟来的Android的Camera开发总结
这是好久前写的项目,但一直没有去总结.刚好在准备找工作这段时间来总结自己做过的东西,学到的东西. 写Android的自定义的相机应用时,首先要知道一些Camera开发必须知道的尺寸,不然在调试的时候, ...
- SNMP协议入门
SNMP协议入门 1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询 ...