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
# graph is represented by adjacency list: List[List[int]] | |
# s: start vertex | |
def dfs_iterating(graph, s): | |
# set is used to mark visited vertices | |
visited = set() | |
# create a stack for DFS | |
stack = [] | |
# Push vertex s to the stack |
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
# graph is represented by adjacency list: List[List[int]] | |
# s: start vertex | |
def dfs(graph, s): | |
# set is used to mark visited vertices | |
visited = set() | |
def recur(current_vertex): | |
print(current_vertex) | |
# mark it visited |
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 | |
# graph: List[List[int]] | |
# s: start vertex | |
def bfs(graph, s): | |
# set is used to mark visited vertices | |
visited = set() | |
# create a queue for BFS | |
queue = deque() |
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 TrieNode(object): | |
def __init__(self): | |
self.children = {} | |
self.is_word = False # mark the end of a word | |
class Trie(object): | |
def __init__(self): |
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: |
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
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 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 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 preorder_traversal_recursive(root): | |
result = [] | |
def recur(node): |