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 ...
随机推荐
- Mybatis:Reader entry: ���� 4
Mybatis:Reader entry: ���� 4 现象: 产生原因:mybatis-config.xml里面配置了包的别名引发的 处理过程:注释掉 结果:就没有乱码了
- ASP.NET写入和读取xml文件
xml是一种可扩展标记语言,在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等.它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 ...
- mongoDB基础知识(一)
mongoDB是一个基于分布式文件存储的数据库,介于关系型数据库和非关系型数据库之间,在非关系型数据库中功能最丰富, 最像关系型数据库.数据结构松散,类似于json的bson格式,可以存储比较复杂的数 ...
- 计算结构体、数组、指针的sizeof
1. 结构体的sizeof 题目: sturct aa{ in num; char name[10];}; struct bb{ int a; float b; struct aa c;}; stru ...
- CODESOFT条码设计软件如何隐藏数据源方法
作为强大的条码标签设计软件,用户在用CODESOFT设计条码标签时,有时需要根据实际情况,将条码数据源隐藏,也就是使设计与打印出来的条形码下不带有数据.那么这要怎么在CODESOFT中实现呢?下面,小 ...
- rosservice call ERROR:Unable to load type ... Have you typed 'make'
you need to source in the new terminal $ source ~/catkin_ws/devel/setup.bash
- MATLAB/OCTAVE常用命令 cheat sheet
MATLAB cheatsheet http://web.mit.edu/18.06/www/Spring09/matlab-cheatsheet.pdf 清除变量 clear 清屏 clc //cl ...
- 正则表达式转换python2的print为python3风格
直接查找 print ([^\n\(]*)替换为 print($1)
- react组件生命周期过程
实例化 首次实例化 getDefaultProps getInitialState componentWillMount render componentDidMount 实例化完成后的更新 getI ...
- ffmpeg处理RTMP流媒体的命令大全
最近浏览国外网站时候发现,翻译不准确的敬请谅解. 1.将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/li ...