Title: Delete Node in a Linked List Source: leetcode.com Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Suppose the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list ...

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ ...

Title: Maximum Depth of Binary Tree Source: leetcode.com Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Python solution Python ''' https://leetcode.com/problems/maximum-depth-of-binary-tree/ ''' # Definition for a binary tree node. # class TreeNode(object): # ...

Title: Same Tree Source: leetcode.com Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Python solution Python ''' https://leetcode.com/problems/same-tree/ ''' # Definition for a binary tree node. # class TreeNode(object): # ...

Title: Sum Root to Leaf Numbers Source: leetcode.com Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / \ 2 3 123 1 / \ 2 ...

Title: Simplify Path Source: leetcode.com Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../"? In this case, you should return "/". Another corner case is the ...

Title: Reverse Linked List II Source: leetcode.com Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. Python solution Python ...

Title: Remove Duplicates from Sorted List II Source: leetcode.com Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3. Python solution Python ''' https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ ''' # Definition for singly-linked list. # class ListNode(object): # def ...

Title: Range Sum Query 2D – Immutable Source: leetcode.com Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) ...