问题:判断二叉树是否为平衡二叉树
分析:树上的任意结点的左右子树高度差不超过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. Azure 7 月新公布

    Azure 7月新发布:Cosmos DB,事件中心捕捉功能,Hybrid Connections,流量管理器快速故障转移功能. 您现有的 DocumentDB 资源现已作为 Azure 门户上 Az ...

  2. javascript:json对象和json字符串的相互转换

    json对象和字符串的相互转换 //使用json中的parser方法转换: var str='{"name":"fendouer", "age&quo ...

  3. oop编程思想

    oop的编程思想:抽象.封装.继承.多态. 1.抽象: 数据抽象:类描述的对象的属性或状态 行为抽象:类描述的对象的行为或功能 举例: 时钟:Class 数据:int Hour,Minute,Seco ...

  4. ztree的数据绑定

    ztree用法(1)首先引用ztree的css和js <link type="text/css" rel="stylesheet" href=" ...

  5. zendstudio 汉化

    http://archive.eclipse.org/technology/babel/index.php http://www.eclipse.org/babel/downloads.php 注册码 ...

  6. 如何处理Eclipse错误消息 The declared package does not match the expected package

    我从github下载了一个开源项目后,导入到自己Eclipse之后,遇到了这个烦人的错误消息: The declared package "com.sap.smartService" ...

  7. CRUD全栈式编程架构总结

    这里放出实例代码 github.com/SkyvenXiong/HCC

  8. 二叉树遍历,先序序列+中序序列=后序序列,Poj(2255)

    这里我参考了JHF大神的写法啦,直接把输出写在了建树的过程中了. 思路: 先根据先序序列找到根节点,在找该节点在中序序列中的位置,这样,左右子树有分开了.这里的细节值得注意一下,不然很容易建树出错.( ...

  9. 1993: C语言实验——最值

    1993: C语言实验——最值 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1541  Solved: 727[Submit][Status][Web ...

  10. python剑指offer数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...