-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
8 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,13 @@ | ||
from typing import TypeAlias | ||
|
||
from src.utils.binary_tree import TreeNode | ||
|
||
Stack: TypeAlias = list[TreeNode | None] | ||
|
||
|
||
class Solution: | ||
def isSameTree(self, p: TreeNode | None, q: TreeNode | None) -> bool: | ||
stack_p: Stack = [p] | ||
stack_q: Stack = [q] | ||
|
||
while stack_p and stack_q: | ||
node_p = stack_p.pop() | ||
node_q = stack_q.pop() | ||
|
||
if node_p and node_q: | ||
if node_p.val != node_q.val: | ||
return False | ||
|
||
stack_p.append(node_p.left) | ||
stack_p.append(node_p.right) | ||
stack_q.append(node_q.left) | ||
stack_q.append(node_q.right) | ||
elif node_p is None and node_q is None: | ||
continue | ||
else: | ||
return False | ||
|
||
return not stack_p and not stack_q | ||
if not p and not q: | ||
return True | ||
elif not p or not q or p.val != q.val: | ||
return False | ||
else: | ||
return self.isSameTree(p.left, q.left) and self.isSameTree( | ||
p.right, q.right | ||
) |