zoukankan      html  css  js  c++  java
  • leetcode--110. Balanced Binary Tree

    1、问题描述

    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.

    2、边界条件:无

    3、思路:先判断两个子树的节点最大深度差是否<=1,若是,则继续检查左右子树是否平衡。

    base case:1)root == null返回true,2)两颗子树的深度差>1,则返回false。

    4、代码实现

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

     方法二:

    For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.

    2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree is balanced, and decides its return value. In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.

    class Solution {
        public boolean isBalanced(TreeNode root) {
            return subTreeDepth(root) != -1;
        }
    
        public int subTreeDepth(TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftDepth = subTreeDepth(root.left);
            if (leftDepth == -1) {
                return -1;
            }
            int rightDepth = subTreeDepth(root.right);
            if (rightDepth == -1) {
                return -1;
            }
            if (Math.abs(leftDepth - rightDepth) > 1) {
                return -1;
            }
            return Math.max(leftDepth, rightDepth) + 1;
        }
    }

    5、api

  • 相关阅读:
    JS制作蔡徐坤打篮球小游戏(鸡你太美?)
    2019-泰迪杯c题数据处理,WGS-84(世界标准地理坐标系) 转为 BD-09(百度地理坐标系)
    浅谈指令系统---(汇编语言)
    PyGame实现情人节表白利器
    Python-王者荣耀自动刷金币+爬取英雄信息+图片
    SSM-网站前台博客系统制作(2)---完善版Google的Kaptcha
    UML 用例之间的关系
    在配置文件web.xml中配置Struts2的启动信息
    IO异常处理
    设计监听器
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7456220.html
Copyright © 2011-2022 走看看