[LeetCode] Balanced Binary Tree 深度搜索
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.
一个深度搜索的问题。
#include <iostream>
#include <stdlib.h>
using namespace std;
/**
* Definition for binary tree
*/
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==NULL) return true;
if(help_fun(root)<) return false;
return true;
}
int help_fun(TreeNode *node)
{
if(node==NULL) return ;
int lft = help_fun(node->left);
int rgt = help_fun(node->right);
if(lft==- ||rgt==-) return -;
if(abs(lft-rgt)<)
return (lft>rgt?lft:rgt) +;
else
return -;
}
}; int main()
{
return ;
}
[LeetCode] Balanced Binary Tree 深度搜索的更多相关文章
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- [Leetcode] Balanced binary tree平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode - Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- [leetcode]Balanced Binary Tree @ Python
原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode Balanced Binary Tree (判断平衡树)
题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过1. 思路:递归解决,但是提供的函数不满足递归的要求啊,我们至少得知道高度,又得返回真假,所以另开个函数解决. / ...
- LeetCode——Balanced Binary Tree
Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...
随机推荐
- 16.1-Jenkins持续集成01—Jenkins服务搭建和部署
分类: Linux架构篇 一.介绍Jenkins 1.Jenkins概念 Jenkins是一个功能强大的应用程序,允许持续集成和持续交付项目,无论用的是什么平台.这是一个免费的源代码,可以处理任何 ...
- ASP.NET Core模块化前后端分离快速开发框架介绍之3、数据访问模块介绍
源码 GitHub:https://github.com/iamoldli/NetModular 演示地址 地址:https://nm.iamoldli.com 账户:admin 密码:admin 前 ...
- mysql四:数据操作
一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...
- 【linux】CPU,内存对网站的影响
如果读写非常多,建议内存大点 如果涉及到的计算非常多,那就升级CPU
- 25.VUE学习之-单击和双击事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android中级教程之Android应用程序的生命周期
Android应用程序的生命周期图 在大部分情况下,每个Android应用都将运行在自己的Linux进程中.当这个应用的某些代码需要执行时,进程就会被创建,并且将保持运行,直到该进程不再需要,而系统需 ...
- 684. Redundant Connection
https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find. class Solu ...
- static关键字 详解
原文地址:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...
- ubuntu版本查看
cat /proc/version uname -a lsb_release -a
- http--一次完整的HTTP事务是怎样一个过程?【转】
一次完整的HTTP事务是怎样一个过程? 如有收获请给作者点赞 --> 原文链接 声明:本文章中的说法仅是个人理解总结,不一定完全正确,但是可以有助于理解. 当我们在浏览器的地址栏输入 www.l ...