[leetcode] 6. Balanced Binary Tree
这个题目纠结了一会儿,终于从二叉树转化到AVL了。题目如下:
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.那么我们在判断的时候就是拿到一个节点然后计算它两边左右子树的最大高度,判断这两者差的绝对值是否大于1就行。这样的话就得先拿递归写个判断树最大高度的函数,然后在判断函数里面调用就可以了。题解如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int MaxDepth(TreeNode *temp)
{
if (temp == NULL)
return 0;
else
{
int aspros = MaxDepth(temp->left);
int defteros = MaxDepth(temp->right);
return 1 + (aspros>defteros ? aspros : defteros);
}
}
bool isBalanced(TreeNode *root)
{
if (root == NULL)
{
return true;
} int aspros = MaxDepth(root->left);
int defteros = MaxDepth(root->right); if (abs(aspros - defteros)>1)
return false;
// This way is wrong!
//if (abs(DepthTree(temp->left) - DepthTree(temp->right) > 1))
// return false;
else
return (isBalanced(root->left) && isBalanced(root->right));
}
};
这次学会了在递归中使用返回值。
[leetcode] 6. Balanced Binary Tree的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- [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. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- LeetCode 110. Balanced Binary Tree (平衡二叉树)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- Leetcode 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- Spring主从数据源动态切换
参考文档: http://uule.iteye.com/blog/2126533 http://lanjingling.github.io/2016/02/15/spring-aop-dynamicd ...
- 北京师范大学第十六届程序设计竞赛决赛-重现赛-B题
一.题目链接 https://www.nowcoder.com/acm/contest/117/B 二.题意 给定一组序列$a_1,a_2,\cdots,a_n$,表示初始序列$b_1,b_2,\cd ...
- PG覆盖率检查
覆盖率检查 需要gcov和lcov工具,gcov在gcc中自带,lcov需要自行下载安装 重新编译 ./configure --prefix=`pwd`/install --with-perl --w ...
- 公司培训lesson 1-代码质量
课堂讲义 代码质量五大原则 编码规范 命名规范.学会合理科学的命名类名.方法名.变量名.命名宗旨:简洁明了,见名只意:了解常用单词缩写 注释.是否写根据所处工作环境的需要而决定.将以源代码写成以“注释 ...
- jquery的html代码中a的onclick的正确显示的代码
jquery的html代码中a的onclick的正确显示的代码 需要转义一下,试了好久才试出来 img_delete.html('<a onclick="deleteImg(\''+s ...
- leetcode204
public class Solution { public int CountPrimes(int n) { ) { ; } ]; ]; ; ; i < n; i++) { mark[i] = ...
- 在MarkDown中插入数学公式对照表(持续更新)
目录 在MarkDown中可以插入数学公式,但是在博客园和有道云笔记之中的数学公式插入方式略有不同(博客园需要先在后台选项中开启插入数学公式选项): 代码 行内公式 整行公式 博客园 $数学公式$ $ ...
- k8s问题记录
1. kubectl run 起来的pod 用 kubectl delete po删不掉 kubectl delete deployment my-nginx kubelet# 看到最后一行:erro ...
- 新手C#参数类型ref、out、params的学习2018.08.04
ref用于传递参数时,将实参传递到函数中,是引用参数,在使用前必须被赋值.string类型也同样适用. static void Main(string[] args) { string a1,a2; ...
- SAP middb主键加索引
alter table DEPTBIGCPDL_DBC drop constraint DEPTBIGCPDL_DBC_PK CREATE UNIQUE INDEX TABLEAU_USER.DEPT ...