Implementing a Priority Heap in Java
This article focuses on implementing a min-heap, which has the property that every parent node is less than or equal to its children. This ensures the smallest element is always at the root (index 1 in our array).
Heap Operations
A min-heap implementation should support these basic operations:
Insertion (I): Add a new element to the heap while ...
Posted on Fri, 17 Jul 2026 16:48:25 +0000 by Gonwee
Understanding Recursion Termination Conditions for Binary Tree Path Problems
LeetCode 257: Binary Tree All Paths
Termination Condition Considerations
The statement if (root == nullptr) return; serves multiple critical purposes in recursive tree algorithms:
Primary Function Guard
When placed in the main function provided by LeetCode, this check handles the empty tree case. If an empty tree is passed to the main function, ...
Posted on Sun, 12 Jul 2026 16:49:45 +0000 by knickerlas
Calculating Depth and Node Count in Binary and N-ary Trees
Maximum Depth of a Binary Tree
Given a binary tree, determine its maximum depth - the number of nodes along the longest path from the root node to the farthest leaf node.
Recursive Approach
Using postorder traversal (left-right-root) to calculate node height:
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(i ...
Posted on Tue, 07 Jul 2026 17:24:08 +0000 by AbraCadaver
Three LeetCode Problems: Binary Tree Split, Array Reduction, and Jump Game
Maximum Product of Splitted Binary Tree
Given a binary tree with root node, remove exactly one edge to split the tree into two separate subtrees. The goal is to maximize the product of the sums of both resulting subtrees. Return the result modulo 10^9 + 7.
Approach
The key insight is that during a depth-first search that calculates subtree sums ...
Posted on Mon, 29 Jun 2026 16:28:43 +0000 by sapoxgn
Constructing a Binary Tree from a String and Performing Traversal Sequences
Given the input string abcdefghij, construct a binary tree by inserting characters level by level — that is, fill nodes row-wise from left to right. The resulting tree must yield the following traversal outputs:
In-order: hdibjeafcg
Post-order: hidjebfgca
Level-order: abcdefghij
The construction follows breadth-first insertion: the first char ...
Posted on Sun, 28 Jun 2026 18:09:32 +0000 by tinkertron
Optimizing Binary Tree Diameter Calculation with Recursive Depth Analysis
The objective is to compute the diameter of a given binary tree. In this context, the diameter is defined as the length of the longest path between any two nodes within the structure. This path does not necessarily need to pass through the root node. The length of a path is quantified by the number of edges connecting the nodes.
Algorithmic Str ...
Posted on Sun, 21 Jun 2026 17:14:22 +0000 by bobob
Segment Tree Implementation for Range Queries and Updates
The segment tree is constructed recursively. Each node tracks its segment boundaries [left, right]. Leaf nodes correspond to individual array elements, while enternal nodes store the sum of their children.
struct SegmentTree {
int left[MAX_N * 4], right[MAX_N * 4];
long long value[MAX_N * 4], lazy[MAX_N * 4];
void build(int l, int ...
Posted on Thu, 18 Jun 2026 17:26:51 +0000 by hkothari
Binary Tree Algorithms: Bottom-Left Value, Path Sum Variants, and Tree Construction from Traversals
Finding the Bottom-Left Node Value
Given the root of a binary tree, return the value of the leftmost node at the deepest level.
Breadth-First Search (Iterative)
A level-order traversal naturally visits nodes layer by layer. The first node encountered in the final level is the answer.
#include <queue>
int findBottomLeftValue(TreeNode* roo ...
Posted on Sat, 13 Jun 2026 17:35:17 +0000 by eideticmnemonic
Binary Tree Construction and Traversal Algorithms in C
Binary Tree Implementation with Extended Preorder Input
This C program constructs a binary tree from an extended preorder sequence where missing children are marked with asterisks. It outputs postorder and inorder traversals and counts nodes with two children.
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
cha ...
Posted on Wed, 10 Jun 2026 17:39:42 +0000 by Anco
Binary Tree Algorithms: Common Interview and OJ Problem Solutions
Preorder Traversal Implementation
Implementing preorder traversal for LeetCode requires attention to specific interface requirmeents. The function signature expects dynamically allocated memory for the result array and a pointer to track the number of elements.
int getNodeCount(struct TreeNode* node) {
if (node == NULL) {
return 0;
...
Posted on Sat, 16 May 2026 19:24:47 +0000 by Flying Sagittarius