TreeDepth
Examples
open all close allBasic Examples (2)
Scope (3)
TreeDepth[[image]]Find the depth of a literal expression in a tree:
tree = [image];TreeDepth[tree, Times]If the expression appears in multiple nodes, TreeDepth gives the maximum depth:
TreeDepth[tree, Power]Find the depth of an expression matching an arbitrary pattern:
TreeDepth[[image], n_ ? Negative]Properties & Relations (6)
TreeDepth[tree] is generally equivalent to TreeDepth[tree,_]:
TreeDepth[[image]]TreeDepth[[image], _]In TreeDepth[tree], empty lists of children contribute one level:
TreeDepth[Tree[data, {}]]TreeDepth[Tree[data, {}], _]TreeDepth[Tree[{tree1,tree2,…}]] gives 1+Max[TreeDepth[tree1],TreeDepth[tree2],…]:
TreeDepth[[image]]% === 1 + Max[TreeDepth[[image]], TreeDepth[[image]]]TreeDepth[Tree[data,None]] gives 0:
TreeDepth[[image]]TreeDepth[Tree[data,{}]] gives 1:
TreeDepth[[image]]TreeDepth can be computed using TreeFold:
TreeDepth[[image]]% === TreeFold[{1 + Max@@#2&, 0&}, [image]]Depth[expr] is equivalent to 1+TreeDepth[ExpressionTree[expr,"Subexpressions"]]:
Depth[f[g, h[1]][x[2, 3], y[z]]]ExpressionTree[f[g, h[1]][x[2, 3], y[z]], "Subexpressions"]1 + TreeDepth[%]Depth[expr,HeadsTrue] is equivalent to 1+TreeDepth[ExpressionTree[expr,"Subexpressions",HeadsTrue]]:
Depth[f[g, h[1]][x[2, 3], y[z]], Heads -> True]ExpressionTree[f[g, h[1]][x[2, 3], y[z]], "Subexpressions", Heads -> True]1 + TreeDepth[%]Related Guides
Text
Wolfram Research (2021), TreeDepth, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeDepth.html (updated 2022).
CMS
Wolfram Language. 2021. "TreeDepth." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/TreeDepth.html.
APA
Wolfram Language. (2021). TreeDepth. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeDepth.html
BibTeX
@misc{reference.wolfram_2026_treedepth, author="Wolfram Research", title="{TreeDepth}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TreeDepth.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treedepth, organization={Wolfram Research}, title={TreeDepth}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeDepth.html}, note=[Accessed: 13-June-2026]}