第四道树题,逐渐能写递归了。虽然最后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的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 ...

  3. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  4. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. [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 ...

  6. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  7. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  8. 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 ...

  9. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

随机推荐

  1. sikuli

    1.sikuli和selenium集成问题,用java封装一个方法去操作web页面上的一些无法定位的控件  http://bbs.csdn.net/topics/390720479/ 2.关于Siku ...

  2. springmvc笔记(基本配置,核心文件,路径,参数,文件上传,json整合)

    首先导入jar包 大家注意一下我的springmvc,jackson,common-up的jar包版本.其他版本有可能出现不兼容. src文件: webroot目录: web.xml <?xml ...

  3. 找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)"的解决方案

    找不到方法"Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)" http://www.microsoft.com/down ...

  4. 蓝桥杯---汉字取首字母(位运算 & 水题)

    确实题目虽然有点水,但是开始的时候好像还真的没有想到怎么提取出这个编号一不小心感觉可以可以用unsigned char 这种类型,直接转为16进制,但是之后发现虽然第一次在codeblock中还行,但 ...

  5. getdata

    public partial class GetData : System.Web.UI.Page { protected void Page_Load(object sender, EventArg ...

  6. ios 对齐属性

    四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft      = 0,    // 左对齐NSTextAlignme ...

  7. HDFS Java API 常用操作

    package com.luogankun.hadoop.hdfs.api; import java.io.BufferedInputStream; import java.io.File; impo ...

  8. HIVE配置文件

    进入HIVE_HOME/conf 编辑文件hive-site.xml,内容如下:(这是伪分布式模式) 主要声明了以下几个内容: 数据仓库地址 数据库连接地址 数据库连接驱动 数据库连接用户名 数据库连 ...

  9. VC获取并修改计算机屏幕分辨率(MFC)

    //检测当前分辨率 int Width = GetSystemMetrics(SM_CXSCREEN); int Height = GetSystemMetrics(SM_CYSCREEN); DEV ...

  10. Oracle 存储过程包

    create or replace package body cuttoship_lots is procedure prod_run(p_w_day date) as begin delete cu ...