[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 用于拖动对象时跟随鼠标的缓动效果
package com.fylibs.components.effects { import flash.display.DisplayObject; import flash.events.Even ...
- Eclipse is running in a JRE, but a JDK is required 解决方法
本文非原创,转自http://liguoliang.com/2010/eclipse-is-running-in-a-jre-but-a-jdk-is-required/ 安装Maven后每次启动出现 ...
- 关于 C/C++ 的文章
关于 C/C++ 的文章,以前写的博客在百度,百度关了,只能一个一个复制了,百度太.....
- 关于显示gif的一些方法与讨论
http://www.2cto.com/kf/201404/292468.html http://www.eoeandroid.com/forum.php?mod=viewthread&tid ...
- Unity协程(Coroutine)原理深入剖析(转载)
记得去年6月份刚开始实习的时候,当时要我写网络层的结构,用到了协程,当时有点懵,完全不知道Unity协程的执行机制是怎么样的,只是知道函数的返回值是IEnumerator类型,函数中使用yield r ...
- Openstack-Mitaka Ceilometer 部署心得
Openstack-Mitaka Ceilometer 部署心得 标签 : Openstack Ceilometer 是 Openstack 的监控管理计费模块,我所用的版本为 Mitaka 版本.C ...
- Type-base dispatch
In the previous section we added two Time objects, but you also might want to add an integer to a Ti ...
- 重拾qt
最近公司又接了一个煤矿的项目,要写个小程序摘取数据,我是公司唯一c++程序员,本来搞ios搞好好的,现在又得重拾半年没摸得qt了.呵呵...呵呵呵. 这里只记录这次小程序的一些小的总结吧.. 1.中文 ...
- Socket连接
socket中TCP的三次握手建立连接详解 我们知道tcp建立连接要进行“三次握手”,即交换三个分组.大致流程如下: 客户端向服务器发送一个SYN J 服务器向客户端响应一个SYN K,并对SYN J ...
- Windows API学习---插入DLL和挂接API
插入DLL和挂接API 在Microsoft Windows中,每个进程都有它自己的私有地址空间.当使用指针来引用内存时,指针的值将引用你自己进程的地址空间中的一个内存地址.你的进程不能创建一个其引用 ...