Skip to content

Commit

Permalink
problems: trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwin B NAIR committed Jul 6, 2023
1 parent b3a254e commit 5f08b85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 100_Same Tree/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p == None and q == None:
return True
elif p == None or q == None:
return False
if(q.val == p.val):
return self.isSameTree(p.right, q.right) and self.isSameTree(p.left, q.left)
14 changes: 14 additions & 0 deletions 104_Maximum Depth Binary Tree/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
elif root.right == None and root.left == None:
return 1
else:
leftHeight = 0
rightHeight = 0
if root.right != None:
rightHeight = self.maxDepth(root.right)
if root.left != None:
leftHeight = self.maxDepth(root.left)
return max(leftHeight, rightHeight) + 1

0 comments on commit 5f08b85

Please sign in to comment.