问题:判断二叉树是否为平衡二叉树
分析:树上的任意结点的左右子树高度差不超过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的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. Leetcode 笔记 110 - Balanced Binary Tree

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

  3. 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 ...

  4. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. [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, ...

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  7. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. 你真的会用ABAP, Java和JavaScript里的constructor么?

    如果constructor里调用了一个成员方法,这个方法被子类override了,当初始化一个子类实例时,父类的构造函数被的调用,此时父类构造函数的上下文里调用的成员方法,是父类的实现还是子类的实现? ...

  2. 虚拟内存映射 段分割 vm_area_struct

    http://www.cnblogs.com/huxiao-tee/p/4660352.html linux内核使用vm_area_struct结构来表示一个独立的虚拟内存区域,由于每个不同质的虚拟内 ...

  3. 整数N分解,搭积木,离散数学中的母函数,ZOJ(1163)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1163 解题报告: 将整数N分解为:两个及以上的不重复的整数,最流 ...

  4. vuejs作用域插槽

    作用域插槽 <div id='root'> <child> <template slot-scope='props'> <h1>{{props.item ...

  5. 起一个node服务

    使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...

  6. Python 连接、操作数据库

    使用python3+pymysql 一.安装python3 a)         从网上下载安装吧 二.安装pymysql https://pypi.python.org/pypi/PyMySQL h ...

  7. 2017.10.5 Java图形化界面设计——布局管理器

    1.BorderLayout(边界布局) 边界布局管理器把容器的的布局分为五个位置:CENTER.EAST.WEST.NORTH.SOUTH.依次对应为:上北(NORTH).下南(SOUTH).左西( ...

  8. 剑指offer 33 把数组排成最小的数

    错误代码 class Solution { public: int FindGreatestSumOfSubArray(vector<int> array) { int length = ...

  9. 物流管理系统(数据库+后台+springMVC+Mybatis+layui)

    数据库:mysql create database WBG_logistics; use WBG_logistics; #1管理员表 create table admin( a_id int prim ...

  10. C# 变量与常量

    变量表示存储位置,变量必须有确定的数据类型.C#的类型安全的含义之一就是确保变量的存储位置容纳着合适的类型.可以将C#中的变量分为静态变量,实例变量,传值参数,引用参数,输出参数,数组参数和本地变量共 ...