<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
        elif not root.left and not root.right:
            return 0
        # print(root)
        current = root
        temp_left_val = None
        temp_right_val = None
        root_list = []
        left_node = []
        left_list = [0]
        res = 0
        while True:
            root_list.append(current)
            if current.left or current.right:
                if current.left:
                    if not left_node:
                        left_node.append(current)
                    current = current.left
                    left_list.append(0)
                    left_node.append(current)
                else:
                    current = current.right
            else:
                right_val = 0
                while len(root_list) > 1:
                    print(root_list[-1].val)
                    if root_list[-1] == root_list[-2].right:
                        temp_left_val = None
                        if temp_right_val:
                            right_val = temp_right_val
                            temp_right_val = None
                            if len(left_node) < len(left_list):
                                left_list.pop()
                        root_list.pop()
                        right_val += 1
                        if root_list[-1].left and root_list[-1].right:
                            if right_val > left_list[-1]:
                                temp_left_val = right_val
                                temp_right_val = right_val
                            else:
                                temp_right_val = left_list[-1]
                                temp_left_val = left_list[-1]
                            if res < right_val + left_list[-1]:
                                res = right_val + left_list[-1]
                                print("res",res, left_list)
                        else:
                            if res < right_val:
                                res = right_val
                            temp_left_val = right_val
                    else:
                        if temp_left_val:
                            left_list[-1] = temp_left_val
                            temp_left_val = None
                        left_val = left_list[-1] + 1
                        left_list.pop()
                        left_node.pop()
                        root_list.pop()
                        if root_list[-1] == left_node[-1]:
                            left_list[-1] = left_val
                        else:
                            left_list.append(left_val)
                        if root_list[-1].right:
                            current = root_list[-1].right
                            bool = True
                            temp_right_val = None
                            break
                        else:
                            temp_right_val = left_list[-1]
                    bool = False
                if not left_list:
                    left_list.append(0)
                if len(root_list) == 1 and bool == False:
                    return max(res,left_list[-1])
18300