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.

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(!root) return true; flag = true;
dfs(root,);
return flag; }
int dfs(TreeNode* node, int depth){
if(!flag)
{
return ;
}
if(!node->left && !node->right)
{
return depth;
} int left=depth;
int right=depth;
if(node->left)
{
left = dfs(node->left,depth+);
} if(node->right)
{
right = dfs(node->right,depth+);
} if(abs(left-right)> )
{
flag = false;
}
return max(left,right);
}
private:
bool flag;
};

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

  1. [LeetCode] 110. Balanced Binary Tree_Easy tag: DFS

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

  2. 110.Balanced Binary Tree Leetcode解题笔记

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

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

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

  4. 110. Balanced Binary Tree - LeetCode

    Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

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

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

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

  7. leetcode 110 Balanced Binary Tree(DFS)

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

  8. 【leetcode❤python】110. Balanced Binary Tree

    #-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):#     def _ ...

  9. 110. Balanced Binary Tree

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

随机推荐

  1. windows update失败还原更改,无法开机

    转自  http://jingyan.baidu.com/article/59a015e34c5a73f794886520.html?tj=exp_relate_3&st=2&from ...

  2. 深入php redis pconnect

    深入php redis pconnect pconnect是phpredis中用于client连接server的api. API文档中的一句原文: The connection will not be ...

  3. TypeScript学习笔记(六) - 模块

    本篇将介绍TypeScript里的模块,和使用方法. 在ECMAScript 2015标准里,JavaScript新增了模块的概念.TypeScript也沿用了这个概念. 一.模块的导入和导出 模块在 ...

  4. Java集合类综合

    Java集合类是JDK学习中的一个经典切入点,也是让初学者最初感受到Java魅力的地方之一,你一定不会忘记不需要关心大小的ArrayList,不用自己实现的Queue,和随处可见的HashMap.面试 ...

  5. HDU 1969 Pie(二分,注意精度)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  6. 利用PHP实现页面跳转同时POST传参,CURL不行

    function payto(){ echo "<form style='display:none;' id='form1' name='form1' method='post' ac ...

  7. MTU&MSS

    MTU是Maximum Transmission Unit的缩写,意为最大传输单元,通俗的理解就是在网络上传送的最大数据包,单位是字节. 以太网对数据帧的长度都有一个限制,其最大值为1500,这个特性 ...

  8. 8 ways to improve ASP.NET Web API performance

    ASP.NET Web API is a great piece of technology. Writing Web API is so easy that many developers don’ ...

  9. Solr中Facet用法和Group用法

    Group分组划分结果,返回的是分组结果: Facet分组统计,侧重统计,返回的是分组后的数量: 一.Group用法: //组查询基础配置params.set(GroupParams.GROUP, & ...

  10. multipart/form-data boundary 说明

    含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协议上传文件 rfc1867协议概述,jsp 应用举例,客户端发送内容构造 1.概述在最初 ...