0% found this document useful (0 votes)
3 views3 pages

Trees LeetCode Problems

The document contains Python solutions for various binary tree problems, including checking if two trees are the same, finding the maximum and minimum depth, and determining if a tree is balanced. Each problem is accompanied by a brief description and the corresponding function implementation. The solutions are designed to be efficient and utilize recursion to traverse the binary tree.

Uploaded by

Ayush Nair
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Trees LeetCode Problems

The document contains Python solutions for various binary tree problems, including checking if two trees are the same, finding the maximum and minimum depth, and determining if a tree is balanced. Each problem is accompanied by a brief description and the corresponding function implementation. The solutions are designed to be efficient and utilize recursion to traverse the binary tree.

Uploaded by

Ayush Nair
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

6/26/25, 11:11 AM md2pdf - Markdown to PDF

1. ✅ 100. Same Tree

def isSameTree(p, q):


if not p and not q: return True
if not p or not q or [Link] != [Link]: return False
return isSameTree([Link], [Link]) and isSameTree([Link], [Link])

2. ✅ 104. Maximum Depth of Binary Tree

def maxDepth(root):
if not root: return 0
return 1 + max(maxDepth([Link]), maxDepth([Link]))

3. ✅ 101. Symmetric Tree

def isSymmetric(root):
def isMirror(t1, t2):
if not t1 and not t2: return True
if not t1 or not t2 or [Link] != [Link]: return False
return isMirror([Link], [Link]) and isMirror([Link], [Link])
return isMirror(root, root)

4. ✅ 110. Balanced Binary Tree

def isBalanced(root):
def check(node):
if not node: return 0
left = check([Link])
right = check([Link])
if left == -1 or right == -1 or abs(left - right) > 1: return -1
return 1 + max(left, right)
return check(root) != -1

5. ✅ 226. Invert Binary Tree

def invertTree(root):
if root:

[Link] 1/3
6/26/25, 11:11 AM md2pdf - Markdown to PDF

[Link], [Link] = invertTree([Link]), invertTree([Link])


return root

6. ✅ 111. Minimum Depth of Binary Tree

def minDepth(root):
if not root: return 0
if not [Link]: return 1 + minDepth([Link])
if not [Link]: return 1 + minDepth([Link])
return 1 + min(minDepth([Link]), minDepth([Link]))

7. ✅ 112. Path Sum

def hasPathSum(root, targetSum):


if not root: return False
if not [Link] and not [Link]:
return [Link] == targetSum
return hasPathSum([Link], targetSum - [Link]) or hasPathSum([Link], targetSum - r

 

8. ✅ 617. Merge Two Binary Trees

def mergeTrees(t1, t2):


if not t1 and not t2: return None
if not t1: return t2
if not t2: return t1
[Link] += [Link]
[Link] = mergeTrees([Link], [Link])
[Link] = mergeTrees([Link], [Link])
return t1

9. ✅ 404. Sum of Left Leaves

def sumOfLeftLeaves(root):
if not root: return 0
res = 0
if [Link] and not [Link] and not [Link]:
res += [Link]
return res + sumOfLeftLeaves([Link]) + sumOfLeftLeaves([Link])

[Link] 2/3
6/26/25, 11:11 AM md2pdf - Markdown to PDF

10. ✅ 543. Diameter of Binary Tree

def diameterOfBinaryTree(root):
diameter = 0
def dfs(node):
nonlocal diameter
if not node: return 0
left = dfs([Link])
right = dfs([Link])
diameter = max(diameter, left + right)
return 1 + max(left, right)
dfs(root)
return diameter

[Link] 3/3

You might also like