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 | |
# 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 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
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_last_pos(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
last_position = -1 # keep track of the latest valid mid position | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
last_position = mid | |
left = mid + 1 # continue searching to the 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
def find_first_pos(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
first_position = -1 # keep track of the latest valid mid position | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
first_position = mid | |
right = mid - 1 # continue searching to the 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
def binary_search(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] == target: | |
return mid | |
elif arr[mid] < target: | |
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
from collections import defaultdict | |
def longest_unique_substring(s): | |
longest_length = float('-inf') | |
n = len(s) | |
# empty string has the longest length is 0 | |
if n == 0: | |
return 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
def longest_unique_substring(s): | |
# function to check if the characters in the substring are all unique or not | |
def is_all_unique(start_index, end_index): | |
character_set = set() | |
for z in range(start_index, end_index): | |
if s[z] in character_set: | |
return False | |
character_set.add(s[z]) | |
return True |
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 highestSum(arr, k): | |
highest_sum = float('-inf') | |
n = len(arr) | |
# n must be not smaller than k | |
if n < k: | |
return -1 | |
left = 0 | |
right = 0 |