<p>Given the <code>root</code> of a binary tree, return <em>the length of the <strong>diameter</strong> of the tree</em>.</p>

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

<p>The <strong>length</strong> of a path between two nodes is represented by the number of edges between them.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg" style="width: 292px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the length of the path [4,2,1,3] or [5,2,1,3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1,2]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
</ul>

# 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 diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
        # if not root: return 0
        # self.res = 0
        # def dfs(node):
        #     if not node: return 0
        #     left = dfs(node.left)
        #     right = dfs(node.right)
        #     self.res = max(self.res, left + right)
        #     return max(left, right) + 1
        # dfs(root)
        # # TC: O(N): The DFS function visits each node exactly once,
        # # SC: O(H) height of the tree
        # return self.res

        if not root:
            return 0
        
        # Initialize the diameter to 0
        diameter = 0
        
        # Stack for simulating DFS, with a tuple of (node, visited)
        # We store (node, 0) to indicate the node is being visited the first time
        # We store (node, 1) to indicate we are backtracking after visiting its children
        stack = [(root, 0)]
        
        # Dictionary to store the computed heights of nodes
        node_heights = {None: 0}  # Initialize with None for leaf children
        
        while stack:
            node, state = stack.pop()
            
            if state == 0:
                # First visit, push node back with state = 1, and visit its children
                stack.append((node, 1))  # Mark as visited after children
                if node.left:
                    stack.append((node.left, 0))  # Visit left child
                if node.right:
                    stack.append((node.right, 0))  # Visit right child
            else:
                # On second visit (after children), calculate height
                left_height = node_heights.get(node.left, 0)
                right_height = node_heights.get(node.right, 0)
                
                # Update the node height
                node_heights[node] = 1 + max(left_height, right_height)
                
                # Update the diameter
                diameter = max(diameter, left_height + right_height)
        
        # The diameter is stored in the variable "diameter"
        return diameter
19000