[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 返回它从下 ...
随机推荐
- [ActionScript 3.0] AS3 绘制正八面体(线条)
分析: 将八面体置于3D坐标系中,其中心的坐标位于原点(0,0,0),让八面体的六个顶点恰好位于3D坐标系的x轴.y轴和z轴上,则从八面体的中心到这六个顶点的距离是相等的.我们可以假设这个距离为r,则 ...
- poj 2388 Who's in the Middle
点击打开链接 Who's in the Middle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28324 Acce ...
- Chapter Configuration
Chapter Configuration 在Web.config 或App.config的configuration节,插入如下配置: <configuration> …… <co ...
- AIX配置时间服务器(NTP)
xntpd是关于网络时间协议的守护进程,它遵循了因特网时间服务器的通用标准.在启动 xntpd 时, xntpd 会读取 /etc/ntp.conf 配置文件来确定网络中系统时钟服务器,以 ntp 服 ...
- Cardinality Feedback
该特性主要针对 统计信息陈旧.无直方图或虽然有直方图但仍基数计算不准确的情况, Cardinality基数的计算直接影响到后续的JOIN COST等重要的成本计算评估,造成CBO选择不当的执行计划 O ...
- chrome 修改标签页
插件名称:New Tab Redirect 标签格式:"file:///home/user/index.html"
- QWizard中运行时默认按钮显示英文问题
QWizard中运行时默认按钮在编译前设计界面的时候是显示中文的,运行的时候就变成英文了.. 后来是发现国际化的时候有问题,解决办法如下: 在main.cpp里加: QTranslator* tran ...
- [转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
一.在工程中添加AddressBook.framework和AddressBookUI.framework 二.获取通讯录 1.在infterface中定义数组并在init方法中初始化 ? 1 2 3 ...
- git remove cache
若在提交.gitignore之前,不小心提交了无用的文件入repo,可以用以下命令在repo中去除这些文件 git rm -r --cached <filename or .> git a ...
- eclipse 调试时出现 Error: [Errno 10013]
法1: 端口占用错误.换个端口即可. 新端口 在 8001到15536之间的任意值. 法2: windows下查看哪个程序占用端口 netstat -ano | findstr “8080” 找到p ...