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.

就是去查看一棵树是不是平衡的,一开始对平衡二叉树的理解有错误,所以写错了 ,看了别人的解答之后更正过来了:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
int dep;
checkBalance(root, dep);
}
bool checkBalance(TreeNode * root, int &dep)
{
if(root == NULL){
dep = ;
return true;
}
int leftDep, rightDep;
bool isLeftBal = checkBalance(root->left, leftDep);
bool isRightBal = checkBalance(root->right, rightDep); dep = max(leftDep, rightDep) + ;
return isLeftBal && isRightBal && (abs(leftDep - rightDep) <= );
}
};

pS:感觉这个不应该easy的题目啊  想的时候头还挺疼的。。

用java的时候用上面的方法去做总是无法成功,所以换了一种方法,这个一开始没有想到,是看别人写的,代码如下所示:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null)
return true;
if(root.left == null && root.right == null)
return true;
if(Math.abs(getDep(root.left) - getDep(root.right)) > 1)
return false;
return isBalanced(root.left) && isBalanced(root.right);
} public int getDep(TreeNode node){
if(node == null)
return 0;
else
return 1 + Math.max(getDep(node.left), getDep(node.right));
}
}

LeetCode OJ:Balanced Binary Tree(平衡二叉树)的更多相关文章

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

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

  2. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

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

  3. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  4. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

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

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

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

  6. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

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

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

  8. 【LEETCODE OJ】Binary Tree Postorder Traversal

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

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

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

  10. 【leetcode】Balanced Binary Tree(middle)

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

随机推荐

  1. 瑞丽熵(renyi entropy)

    在信息论中,Rényi熵是Hartley熵,Shannon熵,碰撞熵和最小熵的推广.熵能量化了系统的多样性,不确定性或随机性.Rényi熵以AlfrédRényi命名.在分形维数估计的背景下,Rény ...

  2. 剑指offer 面试6题

    面试6题: 题目:从尾到头打印链表 输入一个链表,从尾到头打印链表每个节点的值. 解题代码: # -*- coding:utf-8 -*- # class ListNode: # def __init ...

  3. Python之函数总结

    一,函数的定义与调用 定义:def 关键词开头,空格之后接函数名称和圆括号(),最后还有一个":". def 是固定的,不能变,必须是连续的def三个字母,不能分开 def 函数名 ...

  4. Kattis - prva 【字符串】

    题意 从上到下 或者 从左到右 组成的长度 >= 2 的字符串 如果遇到 # 就断掉 输出 字典序最小的那一个 思路 只要从上到下 和从左到右 分别遍历一遍,将 长度 >= 2 的字符串 ...

  5. Linux设置默认启动命令行,而不是图形界面

    无论是作为上线的服务器还是开发中的虚拟机来说,都是没有必要启动图形界面的,而图形界面占用的内存还是很大的.枫竹梦本文就介绍如何设置来默认启动命令行. 其实就是设置系统的启动级别. CentOS的设置 ...

  6. php数组函数-array_pop()

    array_pop()函数删除数组中的最后一个元素(出栈). array_pop(array) array:必需.规定数组 返回值:返回数组的最后一个值.如果数组是空,或者不是一个数组,将返回NULL ...

  7. 20145240 《Java程序设计》第三次实验报告

    20145240 <Java程序设计>第三次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.22 实验 ...

  8. 20165101刘天野 2017-2018-2 《Java程序设计》第8周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十二章Java多线程机制 一.进程与线程 1.1.任务调动 大部分操作系统(如Wi ...

  9. 3.6《深入理解计算机系统》笔记(四)虚拟存储器,malloc,垃圾回收【插图】

    概述 ●我们电脑上运行的程序都是使用虚拟存储,跟物理内存根本不搭边. ●既然虚拟内存是在磁盘上的,为什么它又运行这么好,并没有感觉卡顿?这要感谢程序的局部性! ●虚拟存储器的调度是一个操作系统必须做好 ...

  10. python的一些内置函数

    最近看到一些人写的文章里有提到python的描述符__get__,__set__,__del__. 这里我也小小研究了一下,除了这3个之外还加上过程中学习的几个,比如__call__等. __get_ ...