解法一:From top to bottom

 int treeHeight(TreeNode *T)
{
if (T == NULL)
return ; return max(treeHeight(T->left), treeHeight(T->right)) + ;
} bool isBalanced(TreeNode* root)
{
if (root == NULL)
return true; int left_height = treeHeight(root->left);
int right_height = treeHeight(root->right);
if (abs(left_height - right_height) > )
return false; if (!isBalanced(root->left) || !isBalanced(root->right))
return false; return true;
}

解法一递归判断子树是否为平衡二叉树的过程中,重复计算了多次子树的高度,时间复杂度大于O(N)。解法二将这个计算过程优化了,时间复杂度为O(N)。

解法二:From bottom to top

 int dfsTreeHeight(TreeNode *T)
{
if (T == NULL)
return ; int left_height = dfsTreeHeight(T->left);
if (left_height == -)
return -;
int right_height = dfsTreeHeight(T->right);
if (right_height == -)
return -;
if (abs(left_height - right_height) > )
return -; return max(left_height, right_height) + ;
} bool isBalanced(TreeNode* root)
{
if (root == NULL)
return true; return dfsTreeHeight(root) != -;
}

【LeetCode 110_二叉树_遍历】Balanced Binary Tree的更多相关文章

  1. 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

    解法一:非递归 vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NUL ...

  2. 【LeetCode 100_二叉树_遍历】Same Tree

    解法一:递归 bool isSameTree(TreeNode* p, TreeNode* q) { if (p == NULL && q == NULL) return true; ...

  3. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  4. 【Leetcode】【Easy】Balanced Binary Tree

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

  5. 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree

    解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...

  6. 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree

    解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...

  7. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

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

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

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. java内存解析

    ass BirthDate{ private int day; private int month; private int year; public BirthDate(int d;int m,in ...

  2. mysql5.6创建索引导致锁表阻塞查询

    结论:添加索引时,若果有对该表的慢查询,会导致索引添加延时等待   添加索引语句:alter table tb_name add index idx_xx(col_name);   执行添加索引的SQ ...

  3. Exynos4412 IIC总线驱动开发(一)—— IIC 基础概念及驱动架构分析

    关于Exynos4412 IIC 裸机开发请看 :Exynos4412 裸机开发 —— IIC总线 ,下面回顾下 IIC 基础概念 一.IIC 基础概念 IIC(Inter-Integrated Ci ...

  4. LCD1602

    一.关于LCD1602: 在编写LCD1602程序前,我们必须了解其手册上一些非常重要的信息,如果这些信息不能理解透彻,编程可能会遇到或多或少的问题,在此先大致归纳几点. 1.管脚: 1602共16个 ...

  5. /usr/bin/ld: crti.o: No such file: No such file or directory

    Problem : You are running a 64-bit linux system and trying to compile a 32-bit application and you g ...

  6. HTTP-java访问https资源时,忽略证书信任问题,代码栗子

    java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...

  7. 【前端】用javaScript实现实现一个球池的效果

    ballPool 用javaScript实现实现一个球池的效果,实现效果如下所示: 动图: 截图: HTML代码: <!DOCTYPE html> <html > <he ...

  8. javaWeb中JNDI的使用,为什么要加java:comp/env前缀

    转载自(http://blog.csdn.net/guodongsoft/article/details/52399527) 我们在使用JNDI调用某个对象时,会有下述两种方式 context.loo ...

  9. windchill10.0&11.0API_chm版百度云

    windchill10.0版本和11.0版本的javadoc,也就是api 文件内容 windchill10.0.chm版本的 windchill10.0api.chm版本 百度云链接(免费推荐) 链 ...

  10. 数据结构实习 Problem H 迷宫的最短路径

    数据结构实习 Problem H 迷宫的最短路径 题目描述 设计一个算法找一条从迷宫入口到出口的最短路径. 输入 迷宫的行和列m n 迷宫的布局 输出 最短路径 样例输入 6 8 0 1 1 1 0 ...