[leetcode]_Balanced Binary Tree
第四道树题,逐渐能写递归了。虽然最后AC的代码还是看了网络,但是距离成功攻克此类问题仅一步之遥。
题目:一棵树,判断是否为AVL。(AVL条件:树上任意一点的左右子树的高度差<=1)
思路:树依旧用递归的思想。
解题历程:
1、凭感觉,依葫芦画瓢之前看的递归程序。“判断一颗树是否AVL,如果该node != null ,则递归地判断其 leftSubTree 和 rightSubTree是否为AVL。(这个地方错了,在第三块代码中反映出来,不是node != null ,应该是如果node结点是平衡的 , 则递归判断其左右子树是否平衡)
public boolean isBalanced(TreeNode root) {
if(root == null) return true; else return isBalanced(root.left) && isBalanced(root.right);
}
我只写到了这步,但是但是,这个递归程序只能返回true啊,任意一棵树都会返回true。华丽丽地WA。
2、思考是哪里错了,是否直接用isBalanced(root.left)和isBalanced(root.right)不对呢?是否应该在求高度时用递归呢。
一个结点的高度 = MAX(该结点左子树高度 , 该结点右子树高度) + 1;如果左右子树高度差绝对值<= 1 ,返回true,反之。
至此,我修改了程序:
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
else return Math.abs(calHeight(root.left) - calHeight(root.right)) <= 1 ? true : false ; } public int calHeight(TreeNode node) {
if(node == null) return 0;
else return Math.max(calHeight(node.left) , calHeight(node.right)) + 1;
}
Submit。依旧WA。问题出在isBalance()中计算高度,我只计算了根节点root的左右子树的高度,左右子树中任一结点的高度差是否大于1无从得知。
3、上述代码的修改意见,应该就是加上各个结点的高度差判断,但是,我不会写了。%>_<%。网络上的AC代码:
public boolean isBalanced(TreeNode root) {
if(root == null) return true; if(Math.abs(calHeight(root.left) - calHeight(root.right)) > 1 ) return false; else return isBalanced(root.left) && isBalanced(root.right);
} public int calHeight(TreeNode node) {
if(node == null) return 0;
else return Math.max(calHeight(node.left) , calHeight(node.right)) + 1;
}
惊奇地发现,就是我1,2版本代码的并集。再理下思路:
当一棵树root结点为Null时,该树为AVL。
判断该root结点是否平衡,如果不平衡,则直接返回false;如果平衡,则递归地判断该root结点的左右子树是否平衡,如果左右子树都平衡,才返回true。
[leetcode]_Balanced Binary Tree的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- Java for LeetCode 107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
随机推荐
- 数组排序-冒泡排序-选择排序-插入排序-希尔排序-快速排序-Java实现
这五种排序算法难度依次增加. 冒泡排序: 第一次将数组相邻两个元素依次比较,然后将大的元素往后移,像冒泡一样,最终最大的元素被移到数组的最末尾. 第二次将数组的前n-1个元素取出,然后相邻两个元素依次 ...
- affine transformation matrix 仿射变换矩阵 与 OpenGL
变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可采用的变换模型有如下几种:刚性变换.仿射变换.透视变换和非线形变换等,如下图: 参考: ht ...
- turing 项目引用
1.友盟自动更新 2.友盟统计 3.友盟消息推送 http://www.bejson.com/json2javapojo/ 引用bejson 解析JSON生成类,数组 private List< ...
- JQuery基础教程:选择元素(上)
jQuery最强大的特性之一就是它能够简化在DOM中选择元素的任务,DOM中的对象网络与家谱有几分类似,当我们提到网络中元素之间的关系时,会使用类似描述家庭关系的术语,比如父元素.子元素,等等.通过一 ...
- VC++2005下的ADO SQL语句(like,count,distinct)和操作(转)
http://blog.sina.com.cn/s/blog_56fd66a70100hxjf.html http://timke.blog.163.com/blog/#m=0 环境:MFC Dia ...
- tomcat, jdk, eclipse, ant的安装,设置及常见问题
1.tomcat 安装: 安装版:在官方下载tomcat的安装版,根据提示一步步操作,很简单的 解压版:在官方下载tomcat的解压版,放到要安装的目录中解压版即可 同以前的找到设置环境变量的地方. ...
- Orchard官方文档翻译(四) 让Orchard在WebMatrix下工作
原文地址:http://docs.orchardproject.net/Documentation/Working-with-Orchard-in-WebMatrix 想要查看文档目录请用力点击这里 ...
- GLib基础
实用功能 GLib中包含了近二十种实用功能,从简单的字符处理到初学者很难理解的XML解析功能,这里介绍两种较简单的:随机数和计时. 下面代码演示如何产生1-100之间的随机整数和演示如何计算30000 ...
- 发几个Flex的学习资源
书籍: 目前在看两本 <Essential.ActionScript.3.0> <Flex 4 In Action> 还有两本当手册翻阅,非常喜欢Cookbook这种题材的书, ...
- 操作系统是怎么工作的——mykernel环境的搭建
可以参见:https://github.com/mengning/mykernel 首先感谢:http://www.euryugasaki.com/archives/1014 1.搭建实验环境(实验环 ...