DFS

LeetCode 543 Diameter of Binary Tree (Python)

Posted by 小明MaxMing on November 14, 2024

题目

Given the root of a binary tree, return the length of the diameter of the tree.

The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

The length of a path between two nodes is represented by the number of edges between them.

解题思路

dfs求两个子树的深度,直径为两个子树的深度和

代码

class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        def dfs(root):
            l = r = 0
            if root.left:
                l = dfs(root.left)
            if root.right:
                r = dfs(root.right)
            self.res = max(self.res, l + r + 1)
            return max(l, r) + 1

        self.res = 0
        dfs(root)
        return self.res - 1

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅