leetcode_110. Balanced Binary Tree
来源:互联网 发布:2016黑马程序员课程表 编辑:程序博客网 时间:2024/06/04 20:13
题目:
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.
直接的想法就是,比较做右子树的高度。而这个高度就等于此树左右子树最高的树的高度。所以代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int depth(TreeNode* root){ if(root == NULL) return 0; return 1+max(depth(root->left),depth(root->right)); } bool isBalanced(TreeNode* root) { if(root == NULL) return true; return abs(depth(root->left) - depth(root->right))<=1 && isBalanced(root->left) && isBalanced(root->right); }};
阅读全文
0 0
- leetcode_110 Balanced Binary Tree
- leetcode_110. Balanced Binary Tree
- LeetCode_110.Balanced Binary Tree_平衡二叉树
- leetcode_110. Balanced Binary Tree 判断一棵二叉树是否为平衡二叉树
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Balanced Binary Tree
- linux安装tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
- 关于Tomcat 启动 Removing obsolete files from server Could not clean server of obsolete 问题解决
- 正则表达式三 :编译
- 解决Swift项目中use_frameworks!冲突的问题
- 通过有趣的对话来学习面向对象的设计原则
- leetcode_110. Balanced Binary Tree
- mysql用户管理(转自-米阳光-)
- spring-framework-reference翻译 22 Web MVC framework
- javascript apply()与call()方法
- stl中的仿函数
- 小程序
- 设计模式(行为变化)—— 命令模式
- 蔡康永的毒鸡汤,大家喝点
- gyp verb check python checking for Python executable "python2" in the PATH