This file contains hidden or 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
class BSTIterator: | |
def __init__(self, root: TreeNode): | |
self.stack = [] | |
self.curr_node = root | |
def next(self) -> int: | |
while self.curr_node: | |
self.stack.append(self.curr_node) | |
self.curr_node = self.curr_node.left | |
self.curr_node = self.stack.pop() |
This file contains hidden or 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
class Solution: | |
def findTarget(self, root: TreeNode, k: int) -> bool: | |
arr, stack = [], [] | |
curr_node = root | |
while curr_node or stack: | |
if curr_node: | |
stack.append(curr_node) | |
curr_node = curr_node.left | |
else: |
This file contains hidden or 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
class Solution: | |
def kthSmallest(self, root: TreeNode, k: int) -> int: | |
stack = [] | |
curr = root | |
while curr or stack: | |
if curr: | |
stack.append(curr) | |
curr = curr.left | |
else: |
This file contains hidden or 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
class Solution: | |
def lowestCommonAncestor(self, node, p, q): | |
if not node: | |
return None | |
if p.val < node.val < q.val or p.val > node.val > q.val or node.val in (p.val, q.val): | |
return node | |
if p.val < node.val and q.val < node.val: | |
# Both nodes are on current node's left. | |
return self.lowestCommonAncestor(node.left, p, q) | |
# Otherwise both nodes are on current node's right. |
This file contains hidden or 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
class Solution: | |
def isValidBST(self, node, mini=-inf, maxi=inf): | |
if not node: | |
return True | |
if not mini < node.val < maxi: | |
return False | |
left_is_valid, right_is_valid = True, True | |
if node.left: | |
if node.left.val >= node.val: | |
left_is_valid = False |
This file contains hidden or 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
# 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 bstFromPreorder(self, preorder: List[int]) -> TreeNode: | |
root = TreeNode(preorder[0]) | |
This file contains hidden or 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
class Solution: | |
def searchBST(self, root: TreeNode, val: int) -> TreeNode: | |
if not root: | |
return None | |
if root.val == val: | |
return root | |
if root.val > val: | |
# Current val is too high so move left so we get lower vals. | |
return self.searchBST(root.left, val) | |
# Otherwise move right so we get higher vals. |
This file contains hidden or 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
class Solution: | |
def connect(self, node): | |
if not node: | |
return None | |
# For every node... | |
# 1. We will connect it's left child to it's right child. (If it has children). | |
if node.left and node.right: | |
node.left.next = node.right | |
# 2. Connect it's right child to the current node's next's left child. (If it has a next node). | |
if node.next: |
This file contains hidden or 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
class Solution: | |
def flatten(self, root: TreeNode) -> None: | |
# If current node is null, or it's a leaf, we don't do shit. | |
if root is None or (root.left is None and root.right is None): | |
return | |
if root.left: | |
self.flatten(root.left) | |
original_right = root.right | |
root.right = root.left | |
root.left = None |
This file contains hidden or 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
class Solution: | |
def isSymmetric(self, root: TreeNode) -> bool: | |
return self.isMirror(root, root) | |
def isMirror(self, left_node: TreeNode, right_node: TreeNode) -> bool: | |
if not left_node and not right_node: | |
return True | |
if (not left_node) or (not right_node): | |
return False | |
if left_node.val != right_node.val: |
NewerOlder