题目:

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.

思路:

  • 题目的大意是【判断一个二叉树是不是平衡二叉树
  • 首先了解平衡二叉树的概念:只有根节点是,或者左右节点的深度只差不大于1
  • 因为二叉树本身是一个递归的结构,所以二叉树的好多问题使可以用递归解决的,考虑root和root.left以及root.right的关系

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        if(Math.abs(depth(root.left)-depth(root.right)) > 1){
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);
    }
    public int depth(TreeNode root){
        if(root == null){
            return 0;
        }
        return 1+Math.max(depth(root.left),depth(root.right));
    }
}

LeetCode(24)-Balanced Binary Tree的更多相关文章

  1. LeetCode(110) Balanced Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  2. LeetCode(114) Flatten Binary Tree to Linked List

    题目 分析 按要求转换二叉树: 分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树). 发现规则,便容易处理了.得到先序遍历,构造即可. AC代码 /** * De ...

  3. LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal

    题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...

  4. LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal

    题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...

  5. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

  6. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  7. leetcode笔记(二)94. Binary Tree Inorder Traversal

    题目描述 (原题目链接) Given a binary tree, return the inorder traversal of its nodes' values. For example:Giv ...

  8. LeetCode(99) Recover Binary Search Tree

    题目 Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chang ...

  9. LeetCode(98) Validate Binary Search Tree

    题目 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined ...

随机推荐

  1. 剑指Offer——迅雷笔试题+知识点总结

    剑指Offer--迅雷笔试题+知识点总结 情景回顾 时间:2016.9.19 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:迅雷笔试 总体来说,迅雷笔试内容体量不算多,主要 ...

  2. python安装MySQLdb:在windows下或linux下(以及eclipse中pydev使用msqldb的配置方法)

    写的非常好,可以解决问题: windows下:http://blog.csdn.net/wklken/article/details/7253245 linux下:http://blog.csdn.n ...

  3. hashmap简单实例(个人使用经验)

    一.HashMap<int,String>是错误的:因为int是基本类型,而key和value要求是对象,所以要用Integer而不是int.HashMap<String,Objec ...

  4. RabbitMQ消息队列(五):Routing 消息路由

        上篇文章中,我们构建了一个简单的日志系统.接下来,我们将丰富它:能够使用不同的severity来监听不同等级的log.比如我们希望只有error的log才保存到磁盘上. 1. Bindings ...

  5. Linux之使用网络

    Linux有好多命令可以让你方便的使用网络,常见的有ssh,rsync,wget,curl等等,但是telnet等方式并不适用于网络交互的使用,因为它会暴露你的用户名密码等.所以一般使用安全的命令来进 ...

  6. 我眼中的Linux设备树(三 属性)

    三 属性(property)device_type = "memory"就是一个属性,等号前边是属性,后边是值.节点是一个逻辑上相对独立的实体,属性是用来描述节点特性的,根据需要一 ...

  7. STL:STL各种容器的使用时机详解

    C++标准程序库提供了各具特长的不同容器.现在的问题是:该如何选择最佳的容器类别?下表给出了概述. 但是其中有些描述可能不一定实际.例如:如果你需呀处理的元素数量很少,可以虎落复杂度,因为线性算法通常 ...

  8. MinerConfig.java 爬取配置类

    MinerConfig.java 爬取配置类 package com.iteye.injavawetrust.miner; import java.util.List; /** * 爬取配置类 * @ ...

  9. Html.java 存储页面信息类

    Html.java 存储页面信息类 package com.iteye.injavawetrust.miner; /** * 存储页面信息类 * @author InJavaWeTrust * */ ...

  10. (NO.00001)iOS游戏SpeedBoy Lite成形记(十九)

    最后就是要完成前面设定的第3件事:如果玩家赌对了则赢钱,反之输钱. 前面的框架搭的差不多了,所以这里实现起来也就不难了 ;) 首先我们怎么知道用户输入完毕,然后关闭窗口了?只有在这个点上GameSce ...