后序遍历,每个节点只遍历一次。

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool BalanceTree( TreeNode *root,int &depth )
13     {
14         if( root == NULL )
15         {
16             depth = 0;
17             return true;
18         }
19         int left,right;
20         if( BalanceTree(root->left,left) && BalanceTree(root->right,right) )
21         {
22             int dif = left - right;
23             if( dif >= -1 && dif <= 1 )
24             {
25                 depth = max(left,right) + 1;
26                 return true;
27             }
28         }
29         return false;
30     }
31     bool isBalanced(TreeNode *root) {
32         // Start typing your C/C++ solution below
33         // DO NOT write int main() function
34         int depth=0;
35         return BalanceTree(root,depth);
36     }
37 };

转载于:https://www.cnblogs.com/litana/p/3219941.html

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 ...

  2. LeetCode Balanced Binary Tree

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

  3. leetcode -- Balanced Binary Tree TODO

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

  4. Leetcode: Balanced Binary Tree

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

  5. LeetCode:Balanced Binary Tree

    题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  6. [LeetCode]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 平衡二叉树

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

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

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  9. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

最新文章

  1. 基于OpenSSL安全会话的实现
  2. python学习-综合练习五(五人分鱼(优化解)、顺向、反向推导)
  3. 牛客题霸 [ 有重复项数字的所有排列] C++题解/答案
  4. Java高级工程师每日面试题精选,面试经历分享
  5. NB企业级微服务框架
  6. linux网站渗透工具包,ubuntu下安装 kali linux 渗透工具包
  7. Vue入门 ---- 组件通信
  8. vue.js koa2 mysql_nodejs-koa2-mysql-sequelize-jwt
  9. 梦幻西游端游脚本制作教程
  10. windows android ndk开发,Windows系统下配置Android NDK开发环境
  11. 流量魔盒FlowBox 发行的代币是DMC骗局分析
  12. java: 不兼容的类型: java.lang.Long无法转换为java.lang.Intege
  13. 解决最近easyui官网不能访问问题
  14. ❤️React Hooks⭐
  15. StringTokenizer的用法及示例
  16. 电影解说类自媒体如何才能脱颖而出
  17. 专注于速度的公司对混乱一无所知
  18. Java 验证回文串
  19. C#与数据库访问技术总结(三)之 Connection对象的常用方法
  20. set,setenv和export

热门文章

  1. 不明白JVM虚拟机还怎么面试
  2. 【响应式Web前端设计】Register Demo
  3. 机器学习(MACHINE LEARNING) 【周志华版-”西瓜书“-笔记】 DAY13-半监督学习
  4. php一对一模型关联,thinkphp 模型关联一对一排序
  5. 请概述可视化卷积神经网络的中间输出的基本思想。_卷积神经网络为什么能称霸计算机视觉领域?...
  6. 浅析影响网站空间选择的三大因素
  7. 危害网站关键词优化的因素如何避免?
  8. python数学表达式_Python入门笔记——(1)数字与表达式
  9. Java多线程闲聊(一):概论
  10. python3 推荐使用super调用base类方法