zoukankan      html  css  js  c++  java
  • 剑指offer三十九之平衡二叉树

    一、题目

      输入一棵二叉树,判断该二叉树是否是平衡二叉树。

    二、思路

         详解代码。

    三、代码

    public class Solution {
        //判断根节点左右子树的深度,高度差超过1,则不平衡
        public boolean IsBalanced_Solution(TreeNode root) {
            if (root==null) {
                return true;
            }
            int left = getTreeDepth(root.left);
            int right = getTreeDepth(root.right);
            return Math.abs(left-right)>1?false:true;
        }
        //求取节点的深度
        public static int getTreeDepth(TreeNode root) {
            if (root==null) {
                return 0;
            }
            int leftDepth = 1+getTreeDepth(root.left);
            int rightDepth = 1+getTreeDepth(root.right);
            return leftDepth>rightDepth?leftDepth:rightDepth;
        }
    }
    View Code

    -----------------------------------

    参考链接:

    https://www.nowcoder.com/questionTerminal/8b3b95850edb4115918ecebdf1b4d222

  • 相关阅读:
    最大流问题
    字符串的回文与镜像
    字符串的回文与镜像
    Aho
    linux环境变量
    【Centos7】安装memcached
    linux命令后台执行
    ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决
    ubuntu server解决不能访问外网问题
    【Ubuntu 16】安装net-snmp
  • 原文地址:https://www.cnblogs.com/hezhiyao/p/7658038.html
Copyright © 2011-2022 走看看