Skip to content

Commit

Permalink
problem: 0100 same tree
Browse files Browse the repository at this point in the history
  • Loading branch information
cychiu8 committed Jul 13, 2022
1 parent 955d05e commit 6a5cc5f
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 0100 Same Tree/0100 Same Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 0100. Same Tree

* Difficulty: easy
* Link: https://leetcode.com/problems/same-tree/
* Topics: DFS-BFS

# Clarification

1. Check the inputs and outputs
- INPUT:
- Optional[TreeNode]
- Optional[TreeNode]
- OUTPUT:
- boolean

# Solution

### Thought Process

1.
- Implement

```python
# 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: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
def isSame(p,q):
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return True

deq_p = collections.deque()
deq_q = collections.deque()

deq_p.append(p)
deq_q.append(q)

while deq_p and deq_q:
p = deq_p.popleft()
q = deq_q.popleft()

if not isSame(p, q):
return False

if p or q:
deq_p.append(p.left)
deq_p.append(p.right)
deq_q.append(q.left)
deq_q.append(q.right)

return True
```


### Complexity

- Time complexity: O(n)
- Space complexity: O(n)
- to keep a queue

0 comments on commit 6a5cc5f

Please sign in to comment.