link: https://leetcode.com/problems/binary-tree-preorder-traversal/
# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
def preorder(node):
if node is not None:
result.append(node.val)
preorder(node.left)
preorder(node.right)
preorder(root)
return result
# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
stack, result = [], []
node = root
while True:
while node is not None:
result.append(node.val)
stack.append(node)
node = node.left
if not stack:
return result
node = stack.pop()
node = node.right
# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
stack, result = [root], []
while stack:
node = stack.pop()
if node is not None:
result.append(node.val)
stack.append(node.right)
stack.append(node.left)
return result
Learn More →
# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
node = root
while node is not None:
if node.left is None:
result.append(node.val)
node = node.right
else:
# Find the inorder predecessor of current node
pred = node.left
while pred.right is not None and pred.right is not node:
pred = pred.right
# If the right child of inorder predecessor already points to
# the current node, restore the original tree and update the
# current node to it's right child
if pred.right is node:
pred.right = None
node = node.right
# Make current node as right child of its inorder predecessor
else:
result.append(node.val)
pred.right = node
node = node.left
return result
Ref: https://www.796t.com/content/1545897069.html
leetcode
link: https://leetcode.com/problems/
Feb 9, 2024Difficulty: Easy link: https://leetcode.com/problems/linked-list-cycle/ 1. Hash table $O(n)$ runtime, $O(n)$ space 用set紀錄看過的node,如果有重複就代表有cycle。 python # Definition for singly-linked list. # class ListNode:
Dec 28, 2022Difficulty: Medium link: https://leetcode.com/problems/distribute-coins-in-binary-tree/ 1. DFS $O(N)$ runtime, $O(H)$ space 定義dfs函數,計算以node為root的subtree,扣掉每個child node需要的1枚硬幣後,總共會多(或少)幾枚硬幣。 如果硬幣有多,回傳值就是正的;如果硬幣有缺,回傳值就是負的;剛剛好不多不缺的情況,回傳值為0。 dfs函數顯示,node最少還需要幾枚硬幣(缺硬幣的情況),或著node至少要給出幾枚硬幣(多硬幣的情況),才能達成題目要求的硬幣分布。
Nov 26, 2022Difficulty: Medium link: https://leetcode.com/problems/permutations/ 1. Backtracking $O(n!)$ runtime, $O(n)$ space 需要列出所有排列數,可以用backtracking。用take紀錄有沒有拿過nums的第i個元素。 python class Solution: def permute(self, nums: List[int]) -> List[List[int]]:
Nov 26, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up