【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 = 0;return isBalancedDepth(root, depth);}bool isBalancedDepth(TreeNode* root, int &depth){if(root == NULL) return true;int depthl = 0, depthr = 0;bool ans = isBalancedDepth(root->left, depthl) && isBalancedDepth(root->right, depthr) && abs(depthl - depthr) < 2;depth = ((depthl > depthr) ? depthl : depthr) + 1;return ans;}
其他人的代码,对高度多遍历了一遍,会比较慢:
bool isBalanced(TreeNode *root) {if (!root) return true;if (abs(depth(root->left) - depth(root->right)) > 1) return false;return isBalanced(root->left) && isBalanced(root->right);}int depth(TreeNode *node){if (!node) return 0;return max(depth(node->left) + 1, depth(node->right) + 1);}
转载于:https://www.cnblogs.com/dplearning/p/4481895.html
【leetcode】Balanced Binary Tree(middle)相关推荐
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5055 ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 【leetcode】287 寻找重复数(查找)
题目链接:https://leetcode-cn.com/problems/find-the-duplicate-number/ 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 ...
- 【LeetCode】Algorithms 题集(二)
Linked List Cycle 题意: Given a linked list, determine if it has a cycle in it. Follow up: Can you ...
- 【LeetCode】1160. 拼写单词(C++)
1160. 拼写单词(C++) 1 题目描述 2 示例描述 2.1 示例 1 2.2 示例 2 3 解题提示 4 解题思路 5 源码详解(C++) 1 题目描述 给你一份『词汇表』(字符串数组) wo ...
- 【LeetCode】376. 摆动序列(图解)
376. 摆动序列 一.问题 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4, ...
- 【LeetCode】455. 分发饼干(图解)
文章目录 455. 分发饼干 一.问题 二.注意 三.示例 四.算法思路 五.提交代码 六.测试代码 七.补充 455. 分发饼干 一.问题 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是, ...
- 【Leetcode】拿捏链表(四)——160. 相交链表、141. 环形链表、142. 环形链表 II
作者:一个喜欢猫咪的的程序员 专栏:<Leetcode> 喜欢的话:世间因为少年的挺身而出,而更加瑰丽. --<人民 ...
最新文章
- 转载-对js中new、prototype的理解
- 64位win10系统无法安装.Net framework3.5的两种解决方法
- 一个简单的Ajax例子
- 安装spark1.3.1单机环境
- fest556_AssertJ Fest Hamcrest
- SonarQube+Jenkins,搭建持续交付平台
- Android SQLite封装sql语句、查看数据库
- 简单打开.swf文件
- 梁宁《产品思维》之5同理心训练:产品要顺应用户潜意识
- OV7725之AL422B-FIFO及摄像头的驱动原理
- php自学难_php难吗?自学要多久?需要报培训班吗?
- Jenkins 凭据密码忘记获取凭据密码
- Qt编写带频谱的音乐播放器
- 面向对象分析与设计的底层逻辑
- 致敬2020——打工人的成长故事
- 实词和虚词的区别(自然语言处理要用到)
- H5网页播放器EasyPlayer.js如何实现直播视频实时录像?
- 基于matlab的史密斯圆图演示仿真图
- UltraISO和Rufus制作ubantuU盘启动教程
- WIN10打开IE浏览器访问只兼容IE的网站被EDGE浏览器强制关IE的解决方法
热门文章
- Spring (二) OOP V.S AOP
- IE和Firefox在JS方面的不兼容及统一方法总结
- maven setting.xml配置说明
- 透视WPF 应用程序的利器
- javascript设计模式漫谈之使用委托
- 排序算法c语言和oc实现的,几种常用的排序算法,OC实现
- FPGA中建立时间和保持时间不满足如何解决
- 编程珠玑十一章课后题答案
- linux 编译器错误,linux – GHCi – Haskell编译器错误 – /home/user/.ghci归其他人所有,IGNORING...
- 飞机上为啥禁止使用手机?