Diameter of Binary Tree

文章讨论了如何使用深度优先搜索(DFS)判断二叉树是否高度平衡。通过递归计算每个子树的高度并检查平衡条件,解决这个问题。时间复杂度为O(n),空间复杂度取决于树的高度,最坏情况下为O(n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

Given a binary tree, determine if it is height-balanced.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:

Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:

Input: root = []
Output: true

Intuition

The problem is to determine whether a binary tree is balanced or not. A balanced binary tree is defined as a tree in which the heights of the two subtrees of every node never differ by more than 1. The intuition is to perform a depth-first search (DFS) on the binary tree, calculating the height of each subtree at each step, and checking for balance conditions.

Approach

Depth-First Search (DFS):

Define a recursive DFS function that calculates the height of each subtree.
The base case is when the current node is None, in which case the height is 0.
Calculate the heights of the left and right subtrees using recursive calls.
Check for balance conditions:
If the height difference between the left and right subtrees is greater than 1, or if either subtree is unbalanced (indicated by a height of -1), return -1 to indicate imbalance.
Otherwise, return the height of the current subtree.
Return Result:

The final result is whether the height of the entire tree is -1 or not.

Complexity

  • Time complexity:

The time complexity is O(n), where n is the number of nodes in the binary tree. Each node is visited exactly once during the traversal.

  • Space complexity:

The space complexity is O(h), where h is the height of the binary tree. This is because the maximum depth of the recursion stack is equal to the height of the tree. In the average case, for a balanced binary tree, the height is O(log n), making the space complexity O(log n). However, in the worst case of an unbalanced tree, the height could be O(n), resulting in a space complexity of O(n). The space complexity is dominated by the depth of the recursive call stack.

Code

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def dfs(root):
            if not root:
                return 0

            left = dfs(root.left)
            right = dfs(root.right)

            if left == -1 or right == -1 or abs(left - right) > 1:
                return -1

            return 1 + max(left , right)

        return dfs(root) != -1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值