lintcode: Balanced Binary Tree
2016-03-20 23:04
260 查看
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.
参考:
/article/5767604.html
https://haozhou.gitbooks.io/leetcode-java/content/binarytree/binarytree-balanced.html
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.
/** * 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: /** * @param root: The root of binary tree. * @return: True if this Binary tree is Balanced, or false. */ int height(TreeNode *node){ if(node==NULL){ return 0; } return max(height(node->left),height(node->right))+1; } bool isBalanced(TreeNode *root) { // write your code here if(root==NULL){ return true; } int diff=height(root->left)-height(root->right); if(diff>1||diff<-1){ return false; } return isBalanced(root->left) && isBalanced(root->right); } };
参考:
/article/5767604.html
https://haozhou.gitbooks.io/leetcode-java/content/binarytree/binarytree-balanced.html
相关文章推荐
- 超越自我的事
- 《构建之法》读后感
- 全新安装windows10后,引导原来的Ubuntu(没有解决)
- APS PDW and HADOOP
- 布局之merge的使用
- leetcode012 Integer to Roman
- 问题 A: 输入字符串以及输出
- 自定义view之一:自定义验证码控件
- 线程间通信 1全局变量2当前主对话框指针3发消息方式4 AfxBeginThread的返回值(为CWinThread类型的指针)5AfxGetApp
- bzoj 2301: [HAOI2011]Problem b
- Java学习第三周摘要
- Apache commons (Java常用工具包)简介
- oracle、sql server、mysql分页
- javascript
- Java运算符优先级(转)
- 我也是不得不说我的学习能力下降了,这两天都没有完成一个模块
- 工作线程 界面线程
- UE/UI/UCD/UED 区别
- 实现自己的EventBus
- php:正则表达式的基本语法