LC 1022 - Sum of Root To Leaf Binary Numbers

traverse the tree and maintain current number, double the value when approaching next level, and sum the value up when reach leaf. Code class Solution { public: int sumRootToLeaf(TreeNode* root) { int sum = 0; traverse(0, root, sum); return sum; } void traverse(int n, TreeNode* root, int& sum) { n = (n << 1) + root->val; if(root->left) traverse(n, root->left, sum); if(root->right) traverse(n, root->right, sum); if(root->left == nullptr && root->right == nullptr) sum += n; } };

February 24, 2026 · 1 min

LC 100 - Same Tree

check the currnet node value, and then check the status for left and right child with the function itself recursively. Code class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if (p == nullptr && q == nullptr) return true; else if (p == nullptr || q == nullptr) return false; if (p->val != q->val) return false; if ((p->left == nullptr) ^ (q->left == nullptr)) return false; else if (!isSameTree(p->left, q->left)) return false; if((p->right == nullptr) ^ (q->right == nullptr)) return false; else if (!isSameTree(p->right, q->right)) return false; return true; } };

February 21, 2026 · 1 min

LC 1382 - Balance a Binary Search Tree

Approach To rebuild the balacne tree, I stored the inorder traversal in the vector, and then break all the left and right connection (this step is important!), and then connecting them again by spliting the nodes into two clusters having similar number of nodes. Code /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* balanceBST(TreeNode* root) { vector<TreeNode*> inorder; traverse(inorder, root); int n = inorder.size(); for(TreeNode* node: inorder) node->left = node->right = nullptr; return rebuild(inorder, 0, n-1); } void traverse(vector<TreeNode*>& inorder, TreeNode* root) { if(root->left) traverse(inorder, root->left); inorder.push_back(root); if(root->right) traverse(inorder, root->right); } TreeNode* rebuild(const vector<TreeNode*>& inorder, int start, int end) { if(start == end) return inorder[start]; if(end < start) return nullptr; int m = start + (end - start) / 2; TreeNode* curNode = inorder[m]; curNode->left = rebuild(inorder, start, m-1); curNode->right = rebuild(inorder, m+1, end); return curNode; } };

February 9, 2026 · 1 min

LC 3534 - Path Existence Queries in a Graph II

Approach This is a really interseting problem that I have not seen before, we can leverage the “binary lifting” to do efficient searching. It is clear that we can sort the nums first, and when two values are reachable, we always walk to the farest node. For instance when we have 2,4,6,8, and max step is 3, we can only jump from 2->4->6->8 with 3 steps. For 2,4,6,8 with step 5, we can jump 2->6 since it is the maximum reachable node. I find that the maximum reachable tree can be construct in O(N) with two pointer teachqniue, but I did not figure out what is the best way to query from the tree. I was thinking that in the worse case, we would need to jump N times, and for q queriues, we would need O(qN). ...

January 31, 2026 · 3 min

LC 450 - Delete Node in a BST

Approach I was originally thinking that we need to find the delete node, and find its parent, and then do some tree operations to reconnect the tree nodes. However, I saw that ususally people tries to copy the successor or predecessors value instead of removing it. In this case the implementation could be more easy. We recursively calling the “deleteNode”. When no child, return nullptr. When one child, remove current node and return that child. For two child, we replace the current node with its successor or predecessor. ...

January 27, 2026 · 1 min

LC 3812 - Minimum Edge Toggles on a Tree

Approach This problem is not really hard, the most complex part is to handle the edge removal. Since for the leaf node, we always need to flip the edge connect to it, so we can do it first, and remove that edge and node from the tree. Iteratively doing this process until can’t flip. The time and space complexity are both O(V+E). Originally I’m using the adjacency list traversal to check if the node is removed, and this is actually a sqaure-time solution, since the node might be like a star-like tree with many edge, and we many need to count multiple times for the node when approaching from different edge. Therefore, I modifed it to track the degree of each node, and this can be done in constant time. ...

January 17, 2026 · 2 min

LC 865 - Smallest Subtree with all the Deepest Nodes

Approach My idea is to sweep the tree and collect the maxDeep and maxCnt, and then do the second pass to track the count for maxDeep, and return the first node that has cnt reaching maxCnt`. Code in code-1. After this I ask AI to have an optimized version, and it give me code-2, where we can directly store the depest node, and its ancestors. when left and right subtree have the same deep, we can return the current subtree and the deep+1, else we return the best solution among two subtree. ...

January 9, 2026 · 2 min

LC 1339 - Maximum Product of Splitted Binary Tree

Approach I find that any node (except the root), can be split into a differnt subtree, and the remaining nodes are in another subtree. Therefore, all we have to do is to calculate the sum of the subtree, and the overall sum for all nodes, and try to calculate the maximum product. Code class Solution { public: const int mod = 1'000'000'007; int maxProduct(TreeNode* root) { vector<long long> nodeSum; long long sum = traverse(root, nodeSum); nodeSum.pop_back(); long long maxP = LONG_MIN; for(const auto& s: nodeSum) { long long curP = (sum - s) * s; maxP = max(maxP, curP); } return maxP % mod; } long long traverse(TreeNode* root, vector<long long>& nodeSum) { long long sum = 0; if(root->left) sum += traverse(root->left, nodeSum); if(root->right) sum += traverse(root->right, nodeSum); sum += root->val; nodeSum.push_back(sum); return sum; } };

January 7, 2026 · 1 min

LC 1611. Maximum Level Sum of a Binary Tree

Approach Todays daily is quite straight forward, just do level search and sum up the value for each level, and see which is the maximum. Code class Solution { public: int maxLevelSum(TreeNode* root) { vector<int> sum; traverse(sum, root, 0); return max_element(sum.begin(), sum.end()) - sum.begin() + 1; } void traverse(vector<int>& sum, TreeNode* root, int level) { if(level >= (int)sum.size()) sum.resize(sum.size() + 1); sum[level] += root->val; if(root->left) traverse(sum, root->left, level+1); if(root->right) traverse(sum, root->right, level+1); } };

January 6, 2026 · 1 min