【LeetCode 110_二叉树_遍历】Balanced Binary Tree
解法一: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的更多相关文章
- 【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal
解法一:非递归 vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NUL ...
- 【LeetCode 100_二叉树_遍历】Same Tree
解法一:递归 bool isSameTree(TreeNode* p, TreeNode* q) { if (p == NULL && q == NULL) return true; ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【Leetcode】【Easy】Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree
解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
解法一:递归 int minDepth(TreeNode* root) { if (root == NULL) ; if (root->left == NULL) { ; } else if ( ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- JavaScript 事件处理详解
事件绑定与解绑: el.onEventName = function (){} document.getElementById("dom").onclick = function( ...
- Python面试题之Python面向对象编程汇总
面向对象的设计思想是从自然界中来的,因为在自然界中,类(Class)和实例(Instance)的概念是很自然的.Class是一种抽象概念,比如我们定义的Class——Student,是指学生这个概念, ...
- JS对象深入剖析
对象概述 Objects are mutable keyed collections. An object is a container of properties, where a propert ...
- shell进阶教程
背景:就自己常用的shell脚本写作风格,总结了一些知识点.也是作为交接工作的一部分文档.部分内容单独写 #!/bin/sh # shell脚本进阶教程 # 1.常用知识点:变量设置/日期设置/格式化 ...
- Mac下需要安装的一些软件及常用的配置文件
常用软件配置文件 1..gitconfig # This is Git's per-user configuration file. [user] name = 张文 email = zhangwen ...
- 转载 URL短地址压缩算法
文章转载http://www.nowamagic.net/webdesign/webdesign_ShortUrlInTwitter.php /// <summary> /// 生成sal ...
- docker教程目录
为什么要用 Docker 什么是 Docker Docker 镜像 Docker容器的运用 Docker仓库 Docker如何获取镜像 CentOS 安装Docker Docker 列出镜像 Dock ...
- django教程目录
什么是web框架? Do a web framework ourselves MVC和MTV模式 django的流程和命令行工具 Django的配置文件(settings) Django URL (路 ...
- .net的根目录区别
很久没搞.net了,时间一场,全忘记了,倒,,, “~/” 是应用程序根目录“/” 也是表示根目录 “./” 是当前目录“../”表示当前目录的上一级目录
- springboot统一异常处理及返回数据的处理
一.返回code数据的处理 代码: Result.java /** * http请求返回的最外层对象 * Created by 廖师兄 * 2017-01-21 13:34 */ public cla ...