lintcode-平衡二叉树 -93
来源:互联网 发布:在计算机领域 数据是 编辑:程序博客网 时间:2024/06/05 20:26
给定一个二叉树,确定它是高度平衡的。对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1。
样例
给出二叉树 A={3,9,20,#,#,15,7}
, B={3,#,20,15,7}
A) 3 B) 3 / \ \ 9 20 20 / \ / \ 15 7 15 7
二叉树A是高度平衡的二叉树,但是B不是
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution {public: int backtracing(TreeNode *root){ if(!root) return 0; if(!root->left&&!root->right) return 1; int L,R; if((L=backtracing(root->left))==-1) return -1; if((R=backtracing(root->right))==-1) return -1; if(abs(L-R)>1) return -1; return L>R?L+1:R+1; } bool isBalanced(TreeNode *root) { if(!root) return true; return backtracing(root)!=-1; }};
0 0
- lintcode-平衡二叉树 -93
- LintCode 93-平衡二叉树
- LintCode:平衡二叉树
- 平衡二叉树,lintcode
- lintcode平衡二叉树
- LintCode 平衡二叉树
- 平衡二叉树-LintCode
- lintcode--平衡二叉树
- 平衡二叉树-LintCode
- LintCode 平衡二叉树
- Lintcode-递归-93 平衡二叉树
- LintCode之93 平衡二叉树
- LintCode | 93. 平衡二叉树
- LintCode | 93. 平衡二叉树
- LintCode 93.平衡二叉树
- 【LintCode-93】平衡二叉树(Java实现-递归算法)
- lintcode balanced-binary-tree 平衡二叉树
- 平衡二叉树平衡法则
- 如何将样式表加入您的网页
- 我所知道的日本
- Hadoop笔记五之--clusterID INcompatible(不相容)
- 核心动画
- 报表组件 JFreeChart (jfreechart-1.0.19) 的使用
- lintcode-平衡二叉树 -93
- 设置是否显示对象 ,用display 和visibility
- 推送技术-icomet学习和分析
- AlertDialog_对话框_最全用法详解_自定义布局和适配器
- IOS 利用UIWebView和javascript的技术实现打开网页快速定位到某处(id)
- 直接插入排序的三种实现
- 分享下c++字符转换str转wchar_t *或者说是(LPCWSTR)
- 我国农村生产方式的变迁
- 听见老师的笑,我命白了。