Leetcode Algorithms

Input:     1         1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]Output: true
Input:     1         1
/ \
2 2
[1,2], [1,null,2]Output: false
Input:     1         1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]Output: false
# 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 isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p == None and q == None:
return True
elif p == None and q != None:
return False
elif p != None and q == None:
return False
elif p.val != q.val:
return False
else:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

--

--

--

In love with telling stories with data

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Does open source software have a security problem?

How to build a photo feed using .NET and Pusher

What is a SPIKE? SPIKE for what?

Codeforces Application using Flutter

Essential elements of Agile Scrum

Why Cloud Migration, AWS, Azure Had Been So Popular Till Now?

How to be a more productive Software Engineer

Top Seven Object Storage Tools to consider in 2021

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jen-Li Chen

Jen-Li Chen

In love with telling stories with data

More from Medium

Why does Competitive Programming love Data Structures and Algorithms?

‘K’ Closest Numbers(code and explanation)

Sliding Window

LeetCode Patterns Adventure 22 — Same Tree