110Balanced Binary Tree
问题:判断二叉树是否为平衡二叉树
分析:树上的任意结点的左右子树高度差不超过1,则为平衡二叉树。
搜索递归,记录i结点的左子树高度h1和右子树高度h2,则i结点的高度为max(h1,h2)=1,|h1-h2|>1则不平衡
c++
/**
* 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 flag=true;
int dfs(TreeNode *root)
{
if(root==NULL) return true;
int h1,h2;
if(root->left==NULL) h1=0;
else h1=dfs(root->left);
if(root->right==NULL) h2=0;
else h2=dfs(root->right);
if(abs(h1-h2)>1) flag=0;
return max(h1,h2)+1;
}
bool isBalanced(TreeNode *root) {
dfs(root);
return flag;
}
};
javascript:定义一个全局变量,记录某个二叉树是否平衡。
/**
* 题意:判断二叉树是否为平衡二叉树
* 分析:枚举节点判断其左子树的高度和右子树的高度差是否小于1
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
flag = true;
if(root == null) return true;
var left = DFS(root.left);
var right = DFS(root.right);
if(Math.abs(left-right)>1) {
flag = false;
}
return flag;
}; flag = true;
function DFS(root){
if(root == null) return 0;
var left = DFS(root.left) +1;
var right = DFS(root.right) +1;
if(Math.abs(left-right)>1) {
flag = false;
}
return Math.max(left,right);
}
改进:若某个节点已经不平衡了,则直接返回高度为-1,如此便不用重新定义一个变量
/**
* 题意:判断二叉树是否为平衡二叉树
* 分析:枚举节点判断其左子树的高度和右子树的高度差是否小于1
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
if(root == null) return true;
return DFS(root) != -1;
}; function DFS(root){
if(root == null) return 0;
var left = DFS(root.left);
var right = DFS(root.right);
if(left==-1 || right==-1 || Math.abs(left-right)>1) {//-1表示存在不平衡的情况
return -1;
}
return Math.max(left,right) + 1;
}
110Balanced Binary Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- jquery-ui sortable 排序
https://blog.csdn.net/u013066244/article/details/51954198 <link ref="stylesheet" href ...
- 树checkbox选择jquery实例
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- eclipse 中 添加 tomcat后,启动访问时出现404
1.切换到 server view. 2.双击出问题的 Server,出现如下页面. 3.选中Server,右键,点击选项卡的 Publish,就能进行编辑. 4.Server path 选中第二项: ...
- Ubuntu 14.04 软件源服务器集合
http://wiki.ubuntu.com.cn/Template:14.04source 服务器列表 可将 http://cn.archive.ubuntu.com/ubuntu/ 替换为下列任意 ...
- 笨办法学Python(十五)
习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...
- java Date equals 的坑
今天在JDK6上做开发,遇到一个很诡异的问题. Domain中一个实体是Date,称为变量 a, 使用Calendar构造出来的Date,称为变量b, 虽然都是同一天,比如 2016-11-11 00 ...
- 并查集,是否成树,Poj(1308)
思路: 对于每一条新的边的两个端点,是否是属于一颗树,要是的话,就不是一颗树.否则,就合并. 这里要注意的是,不能是森林,我这里WA了两次了.只不过在最后,查看每个节点的祖先是否是同一个就可以了. # ...
- 开源项目托管github步骤
一.在github新建项目,复制到本地更改之后命令提交. 1.进入github主页新建项目:https://github.com/ccyinghua 2.复制项目地址 3.打开git Bash 命令行 ...
- python 多进程,多线程,协程
在我们实际编码中,会遇到一些并行的任务,因为单个任务无法最大限度的使用计算机资源.使用并行任务,可以提高代码效率,最大限度的发挥计算机的性能.python实现并行任务可以有多进程,多线程,协程等方式. ...
- windows下配置kafka
https://blog.csdn.net/evankaka/article/details/52421314