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 diffe
2016-05-20 09:47
656 查看
class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class Solution { public boolean isBalanced(TreeNode root) { if(root==null)return true; int left=treeDepth(root.left); int right=treeDepth(root.right); if(Math.abs(left-right)<=1){//条件 if(isBalanced(root.left)&&isBalanced(root.right))//递归调用isBanlianced(root); return true; } return false; } private int treeDepth(TreeNode root) {//求树的深度 // TODO Auto-generated method stub if(root==null)return 0; return Math.max(treeDepth(root.right), treeDepth(root.left))+1; } }
相关文章推荐
- Node安装文档
- NodeJS学习——events
- NodeJS学习笔记 - Express4.x路由操作
- LeetCode-237.Delete Node in a Linked List
- nodejs最新教程
- leetcode-24-Swap Nodes in Pairs
- nodejs学习心得
- NodeJS学习笔记
- nodejs Controller层 封装
- nodejs npm
- nodejs中module.exports和exports.xx的区别
- node socket onmessage
- Hadoop 2.0 Yarn代码:NodeManager端代码分析_NM端各服务模块的启动
- 在windows环境下基于sublime text3的node.js开发环境搭建(转)
- Failed to obtain address information for hostname mynode1: Name or service not known
- Windows下安装node及基本操作
- nvm 管理你的nodejs版本
- Node.mongoose
- leetcode 之Reverse Nodes in k-Group(22)
- Node.js+Express配置入门教程详解