Simple Tree Traversal

🖋️ Leaf-Similar Trees (Leetcode 872) In short, we are gonna to compare the leaves between two tree. First, we should get the elements from the leaves. # 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 dfs(self, root, list_para): temp = [] curr = root while curr is not None or len(temp) > 0: while curr: temp.append(curr) curr = curr.left // Save all elements on the left side, but // we only want the leaf element, so... curr = temp.pop() if curr.left is None and curr.right is None: list_para.append(curr.val) curr = curr.right // Attention here, we 'move' our 'pointer' to the right side // After we have done all the things we should do on the left side def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: l1 = [] l2 = [] self.dfs(root1, l1) self.dfs(root2, l2) return l1 == l2 Recursion Method class Solution: def dfs(self, root, list_para): if root is None: return if root.left is None and root.right is None: list_para.append(root.val) // **Move** to the left, do it again, **until 'root is None'** self.dfs(root.left, list_para) // Left side finished // **Move** to the right, do it again, **until 'root is None'** self.dfs(root.right, list_para) // Right side finished, all required data is saved in the list_para def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: l1 = [] l2 = [] self.dfs(root1, l1) self.dfs(root2, l2) return l1 == l2

2026-09-10

Two Ways to Reverse a Singly Linked List

0. Structure of a Singly Linked List class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next // 1 -> 2 -> 3 -> 4 -> 5 -> NULL 1. Move pointers def reverseList(head: ListNode) -> ListNode: prev = None curr = head while curr: temp = curr.next # Save next node curr.next = prev # Reverse the pointer prev = curr # Move prev forward curr = temp # Move curr forward return prev # New head of the reversed list 0: 1 -> None 2 -> 3 -> 4 -> 5 -> NULL 1: 2 -> 1 -> None 3 -> 4 -> 5 -> NULL ...

2026-09-10

Leetcode reflection (1)

1732. Find the Highest Altitude There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0. You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point. ...

2026-09-10

Move Zeroes: Understanding Parameter Behavior in Python

🔔 Prelude: When addressing the “Move Zeroes” problem, it is simple to misinterpret Python’s parameter behavior. Remember: A local variable and a parameter sharing the same name does not mean they refer to the same object. 🖋️ Move Zeroes Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. ...

2026-09-10