LeetCode Balanced Binary Tree (判断平衡树)

时间:2022-09-03 09:09:18

题意:如题,平衡树是指任意一个节点(除了叶子),其左子树的高度与右子树的高度相差不超过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:
bool ans;//答案会记录在此。
int DFS(TreeNode* root)
{
if(!root || !ans) return ; //已经错了,不必要再比较下去
int b=DFS(root->right);
int a=DFS(root->left);
if(abs(a - b)>) ans=false;
return max(a,b)+;
} bool isBalanced(TreeNode* root) {
ans=true;//初始化答案
DFS(root);
return ans;
}
};

AC代码

LeetCode Balanced Binary Tree (判断平衡树)的更多相关文章

  1. LeetCode: Balanced Binary Tree 解题报告

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

  2. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

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

  3. balanced binary tree(判断是否是平衡二叉树)

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

  4. [leetcode]Balanced Binary Tree @ Python

    原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二 ...

  5. LeetCode——Balanced Binary Tree

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

  6. [Leetcode] Balanced binary tree平衡二叉树

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

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

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

  8. LeetCode - Balanced Binary Tree

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

  9. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

随机推荐

  1. 配置php.ini实现PHP文件上传功能

    本文介绍了如何配置php.ini实现PHP文件上传功能.其中涉及到php.ini配置文件中的upload_tmp_dir.upload_max_filesize.post_max_size等选项,这些 ...

  2. html大牛属性

    <META http-equiv="Content-Type" content="text/htmll;charset=gb2312"> 字体 &l ...

  3. ViewPager 详解(一)---基本入门

    前言:这两天研究研究ViewPager滚动功能,现在很多的app都有用到这个功能,我们的大虾米也有这个模块.要研究就彻底的研究研究,我从不满足于一个功能只是简单的应用,要学就学的彻底,所以我打算将Vi ...

  4. 黑马程序猿————OC在Foundation框架结构和字符串

    ------<a href="http://www.itheima.com" target="blank">Java火车.Android火车.iOS ...

  5. &lbrack;转&rsqb;Blocking Code Injection on iOS and OS X

    Source:http://www.samdmarshall.com/blog/blocking_code_injection_on_ios_and_os_x.html Yesterday I pos ...

  6. 《java&period;util&period;concurrent 包源码阅读》07 LinkedBlockingQueue

    这篇文章来说说稍微复杂一些的LinkedBlockingQueue.LinkedBlockingQueue使用一个链表来实现,会有一个head和tail分别指向队列的开始和队列的结尾.因此Linked ...

  7. CNN 卷积层输入Map大小计算

    对于输出的size计算: out_height=((input_height - filter_height + padding_top+padding_bottom)/stride_height ) ...

  8. centos6安装rabbitmq

    一.安装依赖包 yum install build-essential openssl openssl-devel unixODBC unixODBC-devel make gcc gcc-c++ k ...

  9. tcp 三次握手 四次挥手

    TCP协议中的三次握手和四次挥手 建立TCP需要三次握手才能建立,而断开连接则需要四次挥手. 三次握手,建立连接 首先Client发送连接请求报文,Server端接收连接后回复ACK报文,并为这次连接 ...

  10. Cookie&sol;Session机制详解(转载)

    原文链接:http://blog.csdn.net/fangaoxin/article/details/6952954 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用 ...