diameter_of_binary_tree

 1from utils import TreeNode
 2from typing import Optional
 3
 4
 5# @leet start
 6class Solution:
 7    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
 8        """
 9        To find the diameter of a binary search tree, we want to find the max
10        distance between two points in the tree.
11        For the final diameter that we return, we want to return the sum of
12        its left and right branches.
13        For counting the branches as lengths, we want to return the maximum
14        of its left or right branches + 1.
15
16        So, while we traverse the tree, we keep track of the maximum diameter,
17        which is computed by each node's left + right path.
18
19        And then for each node, we return its longer branch + 1, to count for
20        the diameter of the tree.
21        """
22        diameter = 0
23
24        def longest_path(node):
25            nonlocal diameter
26            if not node:
27                return 0
28            left_path = longest_path(node.left)
29            right_path = longest_path(node.right)
30            diameter = max(diameter, left_path + right_path)
31            return max(left_path, right_path) + 1
32
33        longest_path(root)
34        return diameter
35
36
37# @leet end
38
39
40def test():
41    assert 2 + 2 == 4
class Solution:
 7class Solution:
 8    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
 9        """
10        To find the diameter of a binary search tree, we want to find the max
11        distance between two points in the tree.
12        For the final diameter that we return, we want to return the sum of
13        its left and right branches.
14        For counting the branches as lengths, we want to return the maximum
15        of its left or right branches + 1.
16
17        So, while we traverse the tree, we keep track of the maximum diameter,
18        which is computed by each node's left + right path.
19
20        And then for each node, we return its longer branch + 1, to count for
21        the diameter of the tree.
22        """
23        diameter = 0
24
25        def longest_path(node):
26            nonlocal diameter
27            if not node:
28                return 0
29            left_path = longest_path(node.left)
30            right_path = longest_path(node.right)
31            diameter = max(diameter, left_path + right_path)
32            return max(left_path, right_path) + 1
33
34        longest_path(root)
35        return diameter
def diameterOfBinaryTree(self, root: Optional[utils.TreeNode]) -> int:
 8    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
 9        """
10        To find the diameter of a binary search tree, we want to find the max
11        distance between two points in the tree.
12        For the final diameter that we return, we want to return the sum of
13        its left and right branches.
14        For counting the branches as lengths, we want to return the maximum
15        of its left or right branches + 1.
16
17        So, while we traverse the tree, we keep track of the maximum diameter,
18        which is computed by each node's left + right path.
19
20        And then for each node, we return its longer branch + 1, to count for
21        the diameter of the tree.
22        """
23        diameter = 0
24
25        def longest_path(node):
26            nonlocal diameter
27            if not node:
28                return 0
29            left_path = longest_path(node.left)
30            right_path = longest_path(node.right)
31            diameter = max(diameter, left_path + right_path)
32            return max(left_path, right_path) + 1
33
34        longest_path(root)
35        return diameter

To find the diameter of a binary search tree, we want to find the max distance between two points in the tree. For the final diameter that we return, we want to return the sum of its left and right branches. For counting the branches as lengths, we want to return the maximum of its left or right branches + 1.

So, while we traverse the tree, we keep track of the maximum diameter, which is computed by each node's left + right path.

And then for each node, we return its longer branch + 1, to count for the diameter of the tree.

def test():
41def test():
42    assert 2 + 2 == 4