diff --git a/14. Questions/leetcode 101 - symmetric tree.py b/14. Questions/leetcode 101 - symmetric tree.py new file mode 100644 index 0000000..c75c645 --- /dev/null +++ b/14. Questions/leetcode 101 - symmetric tree.py @@ -0,0 +1,22 @@ +# symmetric tree | leetcode 101 | https://leetcode.com/problems/symmetric-tree/ +# given the root of a binary tree, check whether it is a mirror of itself +# method: recursively compare two copies of the same tree + +# 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 isSymmetric(self, root): + def checkSymm(copy1, copy2): + if copy1 is None and copy2 is None: + return True + if copy1 is None or copy2 is None: + return False + + return (copy1.val == copy2.val) and checkSymm(copy1.left, copy2.right) and checkSymm(copy1.right, copy2.left) + + return checkSymm(root, root) diff --git a/14. Questions/leetcode 102 - level order traversal of tree.py b/14. Questions/leetcode 102 - level order traversal of tree.py new file mode 100644 index 0000000..ca88b4e --- /dev/null +++ b/14. Questions/leetcode 102 - level order traversal of tree.py @@ -0,0 +1,39 @@ +# binary tree level order traversal | leetcode 102 | https://leetcode.com/problems/binary-tree-level-order-traversal/ +# order: from left to right, level by level +# method: breadth first search + +#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: + + # l to r, level by level + def levelOrder(self, root): + res = [] + tempQ = [] + + # queue to track visits + tempQ.append(root) + LtempQ = len(tempQ) + + # keep iterating till: + # the track queue is empty + while LtempQ is not 0: + LtempQ = len(tempQ) + level = [] + for i in range(LtempQ): + node = tempQ.pop(0) # pop this node from queue (visited) + if node is not None: + level.append(node.val) # add this node to the level + tempQ.append(node.left) # add left child to queue (to visit) + tempQ.append(node.right) # add right child to queue (to visit) + if len(level) is not 0: + res.append(level) + + return res + + \ No newline at end of file diff --git a/14. Questions/leetcode 104 - max depth of binary tree.py b/14. Questions/leetcode 104 - max depth of binary tree.py new file mode 100644 index 0000000..1c0c7bb --- /dev/null +++ b/14. Questions/leetcode 104 - max depth of binary tree.py @@ -0,0 +1,26 @@ +# max depth of binary tree | leetcode 104 | https://leetcode.com/problems/maximum-depth-of-binary-tree/ +# given the root of a binary tree, return its maximum depth. +# method: recursively increment left and right count for each new node and return max + +# 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 maxDepth(self, root): + def findDepth(node): + if node is None: + return -1 + + ldepth = findDepth(node.left) + rdepth = findDepth(node.right) + + if ldepth > rdepth: + return ldepth + 1 + else: + return rdepth + 1 + + return findDepth(root) + 1 diff --git a/14. Questions/leetcode 98 - validate binary search tree.py b/14. Questions/leetcode 98 - validate binary search tree.py new file mode 100644 index 0000000..c8f5928 --- /dev/null +++ b/14. Questions/leetcode 98 - validate binary search tree.py @@ -0,0 +1,36 @@ +# validate binary search tree | leetcode 98 | https://leetcode.com/problems/validate-binary-search-tree/ +# Given the root of a binary tree, determine if it is a valid binary search tree (BST). +# method: in-order traversal of a valid bst gives a sorted array +# tip: use `prev` pointer instead of an array to keep space complexity as O(1) + +# 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: + + # initialise a prev pointer + def __init__(self): + self.prev = None + + # in-order traversal (L M R) + # should return a sorted array + def isValidBST(self, root) -> bool: + + # if this node is none, its a leaf + if root is None: + return True + + if not self.isValidBST(root.left): + return False + + if self.prev is not None and self.prev.val >= root.val: + return False + + self.prev = root + + return self.isValidBST(root.right) \ No newline at end of file