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
def find_left_border(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
left_border = -1 # keep track of the latest smaller number | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] < target: | |
left_border = mid | |
left = mid + 1 |
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
def find_right_border(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
right_border = -1 # keep track of the latest larger number | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] <= target: | |
left = mid + 1 | |
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
# Definition for a binary tree node. | |
# class TreeNode(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# recursive version | |
def inorder_traversal_recursive(root): | |
result = [] |
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
def inorder_traversal_iterating(root): | |
result = [] | |
# using stack | |
stack = [] | |
current = root | |
while stack or current: | |
if current: | |
# traverse the left subtree | |
stack.append(current) | |
current = current.left |
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(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
def preorder_traversal_recursive(root): | |
result = [] | |
def recur(node): |
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
def preorder_traversal_iterating(root): | |
if not root: | |
return [] | |
result = [] | |
# using stack | |
stack = [root] | |
while stack: | |
current = stack.pop() | |
# visit node |
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(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
def postorder_traversal_recursive(root): | |
result = [] | |
def recur(node): |
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
def postorder_traversal_iterating(root): | |
if not root: | |
return [] | |
result = [] | |
stack = [root] | |
# get the result in order of node, right, left | |
while stack: | |
current = stack.pop() | |
if current.left: |
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(object): | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
def levelorder_traversal_recursive(root): | |
result = [] | |
def recur(node, level): |
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
from collections import deque | |
def levelorder_traversal_iterating(root): | |
if not root: | |
return [] | |
result = [] | |
# using queue | |
queue = deque([root]) | |
while queue: |