Link to problem Description Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all k...
C/C++ Code Snippets
This post is about some C++ code snippets that might be useful when doing online assessment (and this post might continue to grow). Sometimes we just couldn’t remember how to do a certain operation...
Leetcode 227 - Basic Calculator II
Link to problem Description Given a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero. You may assume that...
Leetcode 129 - Sum Root to Leaf Numbers
Link to problem Description You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf ...
Leetcode 1146 - Snapshot Array
Link to problem Description Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with the given length. Initiall...
Leetcode 2549 - Minimum Time to Repair Cars
Link to problem Description You are given an integer array ranks representing the ranks of some mechanics. ranks$_\text{i}$ is the rank of the i$^\text{th}$ mechanic. A mechanic with a rank r can...
Leetcode 2266 - Relocate Marbles
Link to problem Description You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of...
Leetcode 1995 - Count Special Quadruplets
Link to problem Description Given a 0-indexed integer array nums, return the number of distinct quadruplets (a, b, c, d) such that: nums[a] + nums[b] + nums[c] == nums[d], and a < b <...
Leetcode 2265 - Count Nodes Equal to Average of Subtree
Link to problem Description Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree. Note: The average of ...
Leetcode 121 - Best Time to Buy and Sell Stock
Link to problem Description You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock...