Leetcode::Balanced Binary Tree
后序遍历,每个节点只遍历一次。
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相关推荐
- leetcode - Balanced Binary Tree
题目:Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a ...
- 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 TODO
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 binary t ...
- LeetCode:Balanced Binary Tree
题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- [LeetCode]Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
最新文章
- 基于OpenSSL安全会话的实现
- python学习-综合练习五(五人分鱼(优化解)、顺向、反向推导)
- 牛客题霸 [ 有重复项数字的所有排列] C++题解/答案
- Java高级工程师每日面试题精选,面试经历分享
- NB企业级微服务框架
- linux网站渗透工具包,ubuntu下安装 kali linux 渗透工具包
- Vue入门 ---- 组件通信
- vue.js koa2 mysql_nodejs-koa2-mysql-sequelize-jwt
- 梦幻西游端游脚本制作教程
- windows android ndk开发,Windows系统下配置Android NDK开发环境
- 流量魔盒FlowBox 发行的代币是DMC骗局分析
- java: 不兼容的类型: java.lang.Long无法转换为java.lang.Intege
- 解决最近easyui官网不能访问问题
- ❤️React Hooks⭐
- StringTokenizer的用法及示例
- 电影解说类自媒体如何才能脱颖而出
- 专注于速度的公司对混乱一无所知
- Java 验证回文串
- C#与数据库访问技术总结(三)之 Connection对象的常用方法
- set,setenv和export
热门文章
- 不明白JVM虚拟机还怎么面试
- 【响应式Web前端设计】Register Demo
- 机器学习(MACHINE LEARNING) 【周志华版-”西瓜书“-笔记】 DAY13-半监督学习
- php一对一模型关联,thinkphp 模型关联一对一排序
- 请概述可视化卷积神经网络的中间输出的基本思想。_卷积神经网络为什么能称霸计算机视觉领域?...
- 浅析影响网站空间选择的三大因素
- 危害网站关键词优化的因素如何避免?
- python数学表达式_Python入门笔记——(1)数字与表达式
- Java多线程闲聊(一):概论
- python3 推荐使用super调用base类方法