Skip to content

Commit d71d760

Browse files
authored
Merge pull request #1540 from ivanpenaloza/february06
adding algo
2 parents 285ee80 + 0da3725 commit d71d760

6 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
12+
13+
if p and q:
14+
return p.val == q.val \
15+
and self.isSameTree(p.left, q.left) \
16+
and self.isSameTree(p.right, q.right)
17+
else:
18+
return p is q
19+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
12+
def isSymmetric(self, root: TreeNode) -> bool:
13+
return self.check_mirror(root1=root, root2=root)
14+
15+
def check_mirror(self, root1: TreeNode, root2: TreeNode):
16+
17+
if root1 is None and root2 is None:
18+
return True
19+
elif root1 is not None and root2 is not None:
20+
try:
21+
root1.val
22+
root2.val
23+
except:
24+
return False
25+
26+
if root1.val != root2.val:
27+
return False
28+
else:
29+
return self.check_mirror(root1.left, root2.right) \
30+
and self.check_mirror(root1.right, root2.left)
31+
else:
32+
return False
33+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
4+
if (p && q) {
5+
return p.val === q.val
6+
&& isSameTree(p.left, q.left)
7+
&& isSameTree(p.right, q.right);
8+
} else {
9+
return p === q;
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
function isSymmetric(root: TreeNode | null): boolean {
4+
if (!root) return true;
5+
return checkMirror(root, root);
6+
}
7+
8+
function checkMirror(root1: TreeNode | null, root2: TreeNode | null): boolean {
9+
if (root1 === null && root2 === null) {
10+
return true;
11+
} else if (root1 !== null && root2 !== null) {
12+
if (root1.val !== root2.val) {
13+
return false;
14+
} else {
15+
return checkMirror(root1.left, root2.right)
16+
&& checkMirror(root1.right, root2.left);
17+
}
18+
} else {
19+
return false;
20+
}
21+
}
22+
23+
24+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_67_same_tree import Solution, TreeNode
4+
5+
class SameTreeTestCase(unittest.TestCase):
6+
7+
def test_is_same_tree(self):
8+
solution = Solution()
9+
tree1 = TreeNode(1, TreeNode(2), TreeNode(3))
10+
tree2 = TreeNode(1, TreeNode(2), TreeNode(3))
11+
output = solution.isSameTree(p=tree1, q=tree2)
12+
return self.assertTrue(output)
13+
14+
def test_is_no_same_tree(self):
15+
solution = Solution()
16+
tree1 = TreeNode(1, TreeNode(2), TreeNode(3))
17+
tree2 = TreeNode(1, TreeNode(3), TreeNode(2))
18+
output = solution.isSameTree(p=tree1, q=tree2)
19+
return self.assertFalse(output)
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_68_symmetric_binary_tree import Solution, TreeNode
4+
5+
class SymmetricTreeTestCase(unittest.TestCase):
6+
7+
def test_is_symmetric(self):
8+
solution = Solution()
9+
output = solution.isSymmetric(TreeNode(1, TreeNode(7), TreeNode(7)))
10+
self.assertTrue(output)
11+
12+
def test_is_no_symmetric(self):
13+
solution = Solution()
14+
output = solution.isSymmetric(TreeNode(1, TreeNode(2), TreeNode(3)))
15+
self.assertFalse(output)

0 commit comments

Comments
 (0)