110. Balanced Binary Tree (Tree; DFS)
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)的更多相关文章
- [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 ...
- 110.Balanced Binary Tree Leetcode解题笔记
110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 110. Balanced Binary Tree - LeetCode
Question 110. Balanced Binary Tree Solution 题目大意:判断一个二叉树是不是平衡二叉树 思路:定义个boolean来记录每个子节点是否平衡 Java实现: p ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- leetcode 110 Balanced Binary Tree(DFS)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【leetcode❤python】110. Balanced Binary Tree
#-*- coding: UTF-8 -*-#平衡二叉树# Definition for a binary tree node.# class TreeNode(object):# def _ ...
- 110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 201621123006 《Java程序设计》第8周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...
- New Concept English Two 8 19
pls practice every day $课文17 青春常驻 161. My aunt Jennifer is an actress. 我的姑姑詹妮弗是位演员, 162. She must b ...
- 解决Eclipse中文乱码的问题
注意:显示中文所有的编码方式主要是GBK和UTF-8,UTF-8是国际通用的中文编码标准,推荐使用. 一. 设置工作空间的编码 编辑器的编码会影响到所有的项目中的字符的显示,可以说是作用最为广泛的设置 ...
- IE 问题集合
1.zIndex在ie需要 先对比父级的层级
- openssl RSA证书签发命令
一.生成根证书 1. 创建根证书私钥 openssl genrsa -out ca.key 2048 2. 创建根证书申请文件 openssl req -new -key ca.key -out ca ...
- UVA11019 Matrix Matcher【hash傻逼题】【AC自动机好题】
LINK1 LINK2 题目大意 让你在一个大小为\(n*m\)的矩阵中找大小是\(x*y\)的矩阵的出现次数 思路1:Hash hash思路及其傻逼 你把一维情况扩展一下 一维是一个bas,那你二维 ...
- jenkins配置email
# 系统设置 # Jenkins Location # 邮件通知 # 高级 # Failed to send out e-mail 勾选“使用SSL协议” SMTP端口改为465 密码使用授权码,不能 ...
- wiremock docker 运行
使用docker 模式 docker-compose yaml version: '3.3' services: service1: image: rodolpheche/wiremock ports ...
- phpmyadmin不允许一个表创建多个主键的解决办法
在phpmyadmin中执行建表语句 CREATE TABLE `user3` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(2 ...
- 解决hue查询中文报错问题
hue 4.0查询查询中包含中文报一下错误 (1366, Incorrect string value: \\xE4\\xBA\\xAC\\xE4\\xB8\\x9C... for column se ...