Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

题意:给一棵二叉树,判断它是否是平衡二叉树(即每个节点的左右子树深度之差不大于1).

题解:dfs的简单应用。有一个技巧是:用返回-1来表示不行。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode *r){
if(r==NULL){
return ;
}
int ll=dfs(r->left);
int rr=dfs(r->right);
if(rr==-||ll==-||abs(ll-rr)>){
return -;
}
else{
return max(ll,rr)+;
}
}
bool isBalanced(TreeNode* root) {
if(dfs(root)==-){
return false;
}
else{
return true;
}
}
};

leetcode 110 Balanced Binary Tree(DFS)的更多相关文章

  1. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  2. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  3. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  5. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. Leetcode 110. Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. leetcode 110 Balanced Binary Tree ----- java

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. Java [Leetcode 110]Balanced Binary Tree

    题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced b ...

  9. [LeetCode] 110. Balanced Binary Tree 解题思路

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

随机推荐

  1. 使用struts2完成ckeditor和图片上传

    代码地址如下:http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor_ ...

  2. 动态载入Layout 与 论Activity、 Window、View的关系

    1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  3. vscode Python Pylint(代码检测插件)

    暑假刚开始想了解一下Python,使用vscode进行编写,根据vscode 的提示安装了一些不知道干啥的插件,编写过程中提示说  "Linter pylint is not install ...

  4. Linux 下wifi 驱动开发(三)—— SDIO接口WiFi驱动浅析

    SDIO-Wifi模块是基于SDIO接口的符合wifi无线网络标准的嵌入式模块,内置无线网络协议IEEE802.11协议栈以及TCP/IP协议栈.可以实现用户主平台数据通过SDIO口到无线网络之间的转 ...

  5. java 内存与内存溢出

    学习自:http://www.codeceo.com/article/jvm-memory-overflow.html 讲的很清楚

  6. cocos2dx中使用iconv转码(win32,iOS,Android)

    首先贴下环境:Win7 64, NDK r8e, libiconv-1.14, cygwin 一 Win32环境配置 Cocos2D-X自带有win32上的iconv库.仅仅须要配置一下就可以使用. ...

  7. keras----resnet-vgg-xception-inception

    来源: https://www.pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/ classi ...

  8. Centos 7.0设置静态IP

    1.查看NetworkManager.service systemctl | grep "NetworkManager.service" 2.停止NetworkManager.se ...

  9. gridcontrol 之标题 GroupPanel设置 (标题设置,屏蔽右键)

    GroupPanel设置 例如gridcontrol显示标题:“gridcontrol小例子” gridView1.GroupPanelText="gridcontrol小例子"; ...

  10. 我的Java开发学习之旅------>System.nanoTime与System.currentTimeMillis的区别

    首先来看一道题:下面代码的输出结果是什么? import java.util.HashMap; import java.util.Map; public class HashMapTest { pub ...