【LeetCode】110. 平衡二叉树

题目

给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

示例 1:
给定二叉树 [3,9,20,null,null,15,7]

    3
   /   9  20
    /     15   7

返回 true 。

示例 2:
给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      /      2   2
    /    3   3
  /  4   4

返回 false 。

本题同【剑指Offer】面试题55 - II. 平衡二叉树

思路一:自上到下

代码

时间复杂度:O(n^2)
空间复杂度:O(nlogn)

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (!root) {
            return true;
        }
        int left = depth(root->left);
        int right = depth(root->right);
        if (abs(left-right) > 1) {
            return false;
        }
        return isBalanced(root->left) && isBalanced(root->right);
    }
    int depth(TreeNode *root) {
        if (!root) {
            return 0;
        }
        int left = depth(root->left);
        int right = depth(root->right);
        return max(left, right)+1;
    }
};

思路二:自下到上

代码

时间复杂度:O(n)

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if (!root) {
            return true;
        }
        int dfsdepth = depth(root);
        return dfsdepth != -1;
    }
    int depth(TreeNode *root) {
        if (!root) {
            return 0;
        }
        int left = depth(root->left);
        if (left == -1) {
            return -1;
        }
        int right = depth(root->right);
        if (right == -1) {
            return -1;
        }
        if (abs(left-right) > 1) {
            return -1;
        }
        return max(left, right)+1;
    }
};

原文地址:https://www.cnblogs.com/galaxy-hao/p/12392978.html

时间: 2024-11-11 20:13:15

【LeetCode】110. 平衡二叉树的相关文章

[leetcode] 110. 平衡二叉树

110. 平衡二叉树 实际上递归的求每一个左右子树的最大深度即可,如果差值大于1,返回一个-1的状态上去 class Solution { public boolean isBalanced(TreeNode root) { return depth(root)!=-1; } public int depth(TreeNode root) { if (null == root) return 0; int left = depth(root.left); int right = depth(ro

LeetCode 110.平衡二叉树(C++)

给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / 9 20 / 15 7 返回 true . 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / 2 2 / 3 3 / 4 4 返回 false . #include <iostream> #include <numeric>

leetcode——110. 平衡二叉树

class Solution: def isBalanced(self, root: TreeNode) -> bool: if not root: return True def helper(node): if not node: return 0 left=helper(node.left) right=helper(node.right) return max(left,right)+1 return abs(helper(root.left)-helper(root.right))<

Leetcode:110. 平衡二叉树

Leetcode:110. 平衡二叉树 Leetcode:110. 平衡二叉树 点链接就能看到原题啦~ 关于AVL的判断函数写法,请跳转:平衡二叉树的判断 废话不说直接上代码吧~主要的解析的都在上面的链接里了 自顶向下写法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), l

leetCode 110. Balanced Binary Tree 平衡二叉树

110. Balanced Binary Tree 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. 题目大意: 判断一

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

leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法

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. 思路:判断是否是平衡二叉树,dfs即可,递归判断每个子树是否平衡二叉树

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

leetcode 110

110. Balanced Binary Tree 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. 判断二叉树是否为平