https://oj.leetcode.com/problems/balanced-binary-tree/

判断一个二叉树,是否为平衡的。如果是平衡的,则它的每个子树的左右子树高度差不大于1.

递归

但是有两个值,在高层都需要知道。一个是子树是否为二叉树的 bool,一个是子树的高度。所以,一个作为返回值,一个作为引用给传到上面来。

class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root == NULL)
return true;
int p;
return getDepth(root,p);
} bool getDepth(TreeNode *root, int &depth)
{
if(root == NULL)
{
depth = ;
return true;
} if(root->left == NULL && root->right == NULL)
{
depth = ;
return true;
} int l, r;
if(getDepth(root->left, l) == false)
return false;
if(getDepth(root->right, r) == false)
return false; if(abs(l-r)>)
return false;
else
{
//此时,depth需要传到上层去
depth = max(l,r) + ;
return true;
}
}
};

LeetCode OJ-- Balanced Binary Tree ***的更多相关文章

  1. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  3. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  4. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  5. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  6. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  7. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  8. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  9. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  10. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

随机推荐

  1. Java课堂作业

  2. django开发傻瓜教程-1-安装和HelloWorld

    安装 sudo pip install Django 新建项目 django-admin startproject XXX 启动项目 进入主目录下 python manage.py runserver ...

  3. 通过uboot传参设置mtd分区流程源码分析

    因为公司同事反映他使用的开板无法将根目录下的ip_work目mounth成功,由于本人当时没有去现场查看问题,只是象征性的询问内核是否创建了/dev/mtdblock5设备节点,因为该开发板默认是挂载 ...

  4. python PEP8代码规范及问题

    最近刚刚接触Python,为了养成好习惯,尽量保证自己写的代码符合PEP8代码规范,下面是过程中报出的警告及解决方法,英文有些翻译不太准确见谅,会不断更新: PEP 8: module level i ...

  5. NopCommerce 导航菜单HTML静态处理以提高性能

    因网站要快速上线,有时候NopCommerce性能问题一直是困扰我们的最大因素,查找出来需要优化的部分代码进行修改重构是方法之一,我等非主流优化方式只为快速提高程序整体性能. 我以导航菜单为例,列出我 ...

  6. ogre3D学习基础17 --- 如何手动创建ogre程序

    建立自己的Ogre程序 一直以来都是使用ExampleApplication.h来写程序,现在来看看它到底有什么神奇的地方. 首先,我们新建一个win32空项目 然后配置环境 最后新建define.c ...

  7. python字符串内置用法,择选重要

  8. TOJ 4689: Sawtooth

    4689: Sawtooth Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 26     ...

  9. hdu 4176

    Class Statistics Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. eslint规范项目代码

    安装一系列eslint插件后,填写eslint配置,配置如下 .editorconfig root = true [*] charset = utf-8 indent_style = space in ...