LeetCode解题报告 110. Balanced Binary Tree
2016-03-06 09:06
423 查看
110. Balanced Binary Tree
Total Accepted: 100175 TotalSubmissions: 298232 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(E) Maximum Depth of Binary Tree
分析:
以下是错误答案。通过201/226个案例!
/** * 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 maxDepth(TreeNode* node) { if(node==NULL) return 0; return 1+max(maxDepth(node->left),maxDepth(node->right)); } bool isBalanced(TreeNode* root) { if(root==NULL) return true; if(abs(maxDepth(root->left) - maxDepth(root->right)) > 1)//最高根的左子树和右子树节点高度差不能超过1 return false; return true; } };
修改代码后(注意,依然错误):通过案例,218/226
这一次真不知道错在哪里了!
class Solution { public: int maxDepth(TreeNode* node) { if(node==NULL) return 0; return 1+max(maxDepth(node->left),maxDepth(node->right)); } int minDepth(TreeNode* node) { if(node==NULL) return 0; return 1+min(minDepth(node->left),minDepth(node->right)); } bool isBalanced(TreeNode* root) { if(root==NULL) return true; int maxleft = maxDepth(root->left); int maxright = maxDepth(root->right); int minright = minDepth(root->right); int minleft = minDepth(root->left); if(abs(maxleft-minright) > 1)//高度差不能超过1 return false; if(abs(maxright-minleft) > 1)//高度差不能超过1 return false; if(abs(maxleft-minleft) > 1)//高度差不能超过1 return false; if(abs(maxright-minleft) > 1)//高度差不能超过1 return false; return true; } };
参考别人的分析:
题意:
给定一棵二叉树,判断是否是高度平衡的。
高度平衡的意思是,对于每一个节点,左子树跟右子树的高度最多相差1。
思路:
先写一个求高度的函数,递归的求该节点的高度,height(root) = 1 + max(height(root->left),height(root->right))。然后递归判断是否高度平衡,如果当前节点的左子树右子树高度相差小于1,那么递归判断左孩子与右孩子是否满足高度平衡。
以上。
代码如下:
class Solution { public: bool isBalanced(TreeNode* root) { if(root == NULL)return true; int diff = depth(root->left) - depth(root->right); if( diff >= -1 && diff <= 1) return isBalanced(root->left) && isBalanced(root->right); else return false; } int depth(TreeNode* root){ if(root == NULL) return 0; else return max(depth(root->left),depth(root->right)) + 1; } };
以上代码会对同一个节点多次去求高度,算法的复杂度是需要遍历n个节点,并且求每个节点高度时需要平均探索lg(n)的高度,所以复杂度是O(nlgn)。所以我们应该对每个节点只求一次高度,很明显递归时是从下面的节点往上,上面的节点的高度是依靠下面的节点的高度的结果。在求根节点的高度的时候,会去递归求左右孩子的高度,然后取其中一个较大的值再加上1。其实在求左右孩子的高度的时候,可以判断左右子树是否平衡,如果不平衡的话返回高度值是-1代表子树不平衡,所以不是平衡的,那么递归会立即停止执行并向上继续返回-1。如果左右子树都平衡,再去查看左右子树的差值。这样子就不需要重复计算节点的高度。
以上。
代码如下:
class Solution { public: bool isBalanced(TreeNode* root) { if(root == NULL)return true; return (checkHeight(root) > 0); } int checkHeight(TreeNode* root) { if(root == NULL) return 0; int left = checkHeight(root->left); if(left == -1) return -1; int right = checkHeight(root->right); if(right == -1) return -1; int diff = left - right; if(diff > 1 || diff < -1) return -1; return max(left, right) + 1; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50811950
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:/article/3664871.html
参考资源:
【1】博客地址,http://blog.csdn.net/u014673347/article/details/46707609
相关文章推荐
- 第23讲项目3——两段函数求值
- JSON 之 SuperObject(12): TSuperEnumerator、TSuperAvlIterator、ObjectFindFirst...
- 【SQL Server CE2.0】创建加密的数据库(源代码)
- Codeforces Round #344 (Div. 2)
- linux内核权限控制之vfs_permission分析
- Linux调优常用命令
- 【代码笔记】Java基础:Java的方法和类
- 线程的取消/撤销
- D-Professor GukiZ's Robot
- 链表排序
- 8种android 对话框(Dialog)使用方法详解
- HDU 1260 Tickets (简单DP)
- Task.Run Vs Task.Factory.StartNew z
- 【SQL Server CE2.0】打开加密的数据库(源代码)
- 初等数论四大定理之——费马小定理
- 3876: [Ahoi2014]支线剧情 有上下界的费用流
- [LeetCode]318. Maximum Product of Word Lengths
- 第23讲项目2——乱玩数字
- linux中安装easy_install(setuptools)
- 剑指offer题目记录