🐐
LeetGoat
Interview Prep
Competitive Programming
Discord
Tree Query Picker
Range queries
2D range queries
Sets
Union find
Strings
Tree queries
Tree helpers
Graphs
Matrices
Math
Primes
Bit ops & Bitset
Geometry
Misc.
Golden References
Generic tree templates ▾
Unselect all
Tree shape (no values)
Ancestors & distance
lca
(a, b)
Lowest common ancestor of a and b
unweightedDist
(a, b)
Number of edges on the path a..b
depth
(node)
Edges from the root to node (the root is depth 0)
parent
(node)
node's parent (-1 for the root)
kthAncestor
(node, k)
The node k steps above node (k = 0 is node; -1 if past the root)
isAncestor
(a, b)
Is a an ancestor of b? (true when a == b)
subtreeSize
(node)
Number of nodes in node's subtree (node included)
countAtDepthInSubtree
(node, d)
Number of nodes in node's subtree at depth d (the root is depth 0)
As if rooted at r
lcaRootedAt
(r, a, b)
LCA of a and b if the tree were rooted at r instead
parentRootedAt
(r, node)
node's parent if the tree were rooted at r (-1 when node == r)
subtreeSizeRootedAt
(r, node)
Size of node's subtree if the tree were rooted at r
Paths
kthNodeOnPath
(a, b, k)
The kth node walking a -> b (k = 0 is a, k = unweightedDist(a, b) is b; -1 if k is past b)
nextNodeOnPath
(a, b)
The neighbor of a one step toward b (-1 if a == b)
isOnPath
(a, b, node)
Does the path a..b pass through node? (a and b count)
unweightedDistToPath
(a, b, node)
Fewest edges from node to any node on the path a..b (0 if node is on it)
unweightedDistBetweenPaths
(a, b, c, d)
Fewest edges between any node on path a..b and any node on path c..d (0 if they share a node)
median
(a, b, c)
The one node that lies on all three paths between a, b and c
pathIntersection
(a, b, c, d)
The shared part of paths a..b and c..d as its two end nodes (p, q); p == q if they share one node; (-1, -1) if they share none
pathNodes
(a, b)
Every node on the path a..b, in order from a (O(length))
Node values
Edge values
Node queries
One node
nodeGet
(node)
val(node)
Path a..b
Subtree of node
nodePathSum
(a, b)
Sum of the node values on the path a..b
with fixed MOD
nodePathMin
(a, b)
Min node value on the path a..b
nodePathMax
(a, b)
Max node value on the path a..b
nodePathGcd
(a, b)
Gcd of the node values on the path a..b
nodePathMaxSubarraySum
(a, b)
Max sum of a non-empty stretch of the path
nodePathMinSubarraySum
(a, b)
Min sum of a non-empty stretch of the path
nodePathLongestEqualRun
(a, b)
Longest run of equal adjacent node values along the path
Rank
nodePathKthSmallest
(a, b, k)
kth smallest of the node values on the path a..b (1-indexed: k = 1 is the min)
nodePathKthLargest
(a, b, k)
kth largest of the node values on the path a..b (1-indexed: k = 1 is the max)
Count
nodePathCountLtX
(a, b, x)
How many node values on the path a..b are < x
nodePathCountLteX
(a, b, x)
How many node values on the path a..b are ≤ x
nodePathCountGtX
(a, b, x)
How many node values on the path a..b are > x
nodePathCountGteX
(a, b, x)
How many node values on the path a..b are ≥ x
nodePathCountInValRange
(a, b, low, high)
How many node values on the path a..b are in [low, high]
Sum
nodePathSumLtX
(a, b, x)
Sum of the node values on the path a..b that are < x
nodePathSumLteX
(a, b, x)
Sum of the node values on the path a..b that are ≤ x
nodePathSumGtX
(a, b, x)
Sum of the node values on the path a..b that are > x
nodePathSumGteX
(a, b, x)
Sum of the node values on the path a..b that are ≥ x
nodePathSumInValRange
(a, b, low, high)
Sum of the node values on the path a..b in [low, high]
Sum by rank (1-indexed)
nodePathSumSmallestK
(a, b, k)
Sum of the k smallest node values on the path a..b
nodePathSumLargestK
(a, b, k)
Sum of the k largest node values on the path a..b
nodePathSumKthSmallestRange
(a, b, kFrom, kTo)
Sum of the kFrom-th … kTo-th smallest node values on the path a..b
nodePathSumKthLargestRange
(a, b, kFrom, kTo)
Sum of the kFrom-th … kTo-th largest node values on the path a..b
Distance sums
nodePathSumAbsDiffToX
(a, b, x)
Σ |val − x| over the node values on the path a..b
nodePathSumAbsDiffToMedian
(a, b)
Σ |val − median| over the node values on the path a..b (the fewest ±1 steps to make them all equal)
Nearest to x
nodePathSmallestGteX
(a, b, x)
Smallest of the node values on the path a..b that is ≥ x
nodePathSmallestGtX
(a, b, x)
Smallest of the node values on the path a..b that is > x
nodePathLargestLteX
(a, b, x)
Largest of the node values on the path a..b that is ≤ x
nodePathLargestLtX
(a, b, x)
Largest of the node values on the path a..b that is < x
Frequency
nodePathCountOccurrences
(a, b, x)
How many node values on the path a..b equal x
nodePathCountDistinct
(a, b)
Number of distinct node values on the path a..b (bitsets: for up to a few thousand distinct values)
nodePathAllDistinct
(a, b)
Are all the node values on the path a..b distinct? (bitsets: for up to a few thousand distinct values)
nodePathAllEvenFrequency
(a, b)
Does every value appear an even number of times on the path a..b?
▶
Bit ops
▶
Prefix / suffix sums (path is a -> b)
▶
Hashing
Search
nodePathFirstGteX
(a, b, x)
First node walking a -> b with val ≥ x (-1 if none)
nodePathFirstGtX
(a, b, x)
First node walking a -> b with val > x (-1 if none)
nodePathFirstLteX
(a, b, x)
First node walking a -> b with val ≤ x (-1 if none)
nodePathFirstLtX
(a, b, x)
First node walking a -> b with val < x (-1 if none)
nodePathFirstEqX
(a, b, x)
First node walking a -> b with val = x (-1 if none)
nodePathLastGteX
(a, b, x)
Last node walking a -> b with val ≥ x (-1 if none)
nodePathLastGtX
(a, b, x)
Last node walking a -> b with val > x (-1 if none)
nodePathLastLteX
(a, b, x)
Last node walking a -> b with val ≤ x (-1 if none)
nodePathLastLtX
(a, b, x)
Last node walking a -> b with val < x (-1 if none)
nodePathLastEqX
(a, b, x)
Last node walking a -> b with val = x (-1 if none)
nodeNearestAncestorGteX
(node, x)
Nearest ancestor of node (node included) with val ≥ x (-1 if none)
nodeNearestAncestorGtX
(node, x)
Nearest ancestor of node (node included) with val > x (-1 if none)
nodeNearestAncestorLteX
(node, x)
Nearest ancestor of node (node included) with val ≤ x (-1 if none)
nodeNearestAncestorLtX
(node, x)
Nearest ancestor of node (node included) with val < x (-1 if none)
nodeNearestAncestorEqX
(node, x)
Nearest ancestor of node (node included) with val = x (-1 if none)
Node updates
One node
nodeAssign
(node, x)
val(node) = x
nodeAdd
(node, x)
val(node) += x
nodeChmax
(node, x)
val(node) = max(val, x)
nodeChmin
(node, x)
val(node) = min(val, x)
Path a..b
Subtree of node
nodePathAssign
(a, b, x)
val = x for every node on the path a..b
nodePathAdd
(a, b, x)
val += x for every node on the path a..b
with fixed MOD
nodePathChmax
(a, b, x)
val = max(val, x) for every node on the path a..b
nodePathChmin
(a, b, x)
val = min(val, x) for every node on the path a..b
nodePathXORApply
(a, b, x)
val ^= x for every node on the path a..b
nodePathORApply
(a, b, x)
val |= x for every node on the path a..b
nodePathANDApply
(a, b, x)
val &= x for every node on the path a..b
nodePathAddArithSeq
(a, b, start, step)
Walking a -> b, the nodes get start, start + step, ... added
with fixed MOD
nodePathMultMod
(a, b, m)
val = (val · m) % MOD for every node on the path a..b
nodePathAffineMod
(a, b, mult, add)
val = (val · mult + add) % MOD for every node on the path a..b
Still to add:
rerooting for value queries: subtree of node when rooted at r