TreeLevel
✖
TreeLevel
gives a list of the element elem of subtrees on levels specified by levelspec.
Details and Options


- TreeLevel lists the subtrees on any range of levels in a Tree object. The list of subtrees can be given in many different orders, including depth-first and breadth-first traversals.
- TreeLevel[tree,levelspec] gives a list of the subtrees on the given levels. This can also be expressed as TreeLevel[tree,levelspec"Subtree"].
- TreeLevel[tree,levelspec"Data"] gives a list of the data on the given levels.
- TreeLevel uses standard level specifications:
-
n levels 1 through n All levels 0 through Infinity Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2 "Leaves" level -1 only "NonLeaves" levels 0 through -2 - A positive level n+1 consists of all subtrees of tree that are children of subtrees on level n.
- Level 0 corresponds to the root of the tree.
- Level -1 corresponds to the leaves.
- A negative level -(n+1) consists of all subtrees whose children are on levels -1, -2, …, -n, with highest level -n.
- TreeLevel traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeLevel[…,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
- TreeLevel[levelspec][tree] is equivalent to TreeLevel[tree,levelspec].

Examples
open allclose allBasic Examples (3)Summary of the most common use cases
Extract the subtrees on the second level:

https://wolfram.com/xid/0i1mnkxwu-ynsdz6

Extract the data on a range of levels:

https://wolfram.com/xid/0i1mnkxwu-x1c5g2

Use the operator form of TreeLevel:

https://wolfram.com/xid/0i1mnkxwu-x8v6xu

Scope (8)Survey of the scope of standard use cases
Level Specifications (7)
Extract the subtrees on all levels:

https://wolfram.com/xid/0i1mnkxwu-70t03t

Extract the subtrees on levels 1, 2 and 3:

https://wolfram.com/xid/0i1mnkxwu-u64yj5

https://wolfram.com/xid/0i1mnkxwu-o94lnv

Extract the subtrees only on level 3:

https://wolfram.com/xid/0i1mnkxwu-4csw25


https://wolfram.com/xid/0i1mnkxwu-s2gdvt

Extract all levels, starting at level 1:

https://wolfram.com/xid/0i1mnkxwu-eg4ctt


https://wolfram.com/xid/0i1mnkxwu-1vzax2


https://wolfram.com/xid/0i1mnkxwu-effz78

https://wolfram.com/xid/0i1mnkxwu-mnssvw


https://wolfram.com/xid/0i1mnkxwu-ppusdz


https://wolfram.com/xid/0i1mnkxwu-yuv5x2

Positive and negative levels can be mixed:

https://wolfram.com/xid/0i1mnkxwu-feddft


https://wolfram.com/xid/0i1mnkxwu-ylvvnp


https://wolfram.com/xid/0i1mnkxwu-besmsn

Use TreeLevel as an operator form in a tree position specification:

https://wolfram.com/xid/0i1mnkxwu-ym9b5o

Options (3)Common values & functionality for each option
TreeTraversalOrder (3)
By default, subtrees are listed in a depth-first order, with parents visited after their children:

https://wolfram.com/xid/0i1mnkxwu-5tcyv1

Specify a top-down, right-to-left variant:

https://wolfram.com/xid/0i1mnkxwu-17bium

List subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:

https://wolfram.com/xid/0i1mnkxwu-clwpbg

List subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:

https://wolfram.com/xid/0i1mnkxwu-4765gg

Applications (2)Sample problems that can be solved with this function
Properties & Relations (4)Properties of the function, and connections to other functions
TreeLevel[tree,levelspec] is equivalent to TreeChildren@TreeExtract[tree,TreeLevel[levelspec]]:

https://wolfram.com/xid/0i1mnkxwu-rkjw55


https://wolfram.com/xid/0i1mnkxwu-10kek8


https://wolfram.com/xid/0i1mnkxwu-9nlt0t

Level[expr,levelspec] is equivalent to TreeLevel[ExpressionTree[expr,"Subexpressions"],levelspec"Data"]:

https://wolfram.com/xid/0i1mnkxwu-thglzm


https://wolfram.com/xid/0i1mnkxwu-hyca58


https://wolfram.com/xid/0i1mnkxwu-3lofev

Level[expr,levelspec,HeadsTrue] is equivalent to TreeLevel[ExpressionTree[expr,"Subexpressions",HeadsTrue],levelspec"Data"] for positive levels:

https://wolfram.com/xid/0i1mnkxwu-epadq4


https://wolfram.com/xid/0i1mnkxwu-x6ybmf


https://wolfram.com/xid/0i1mnkxwu-hsc0zr

TreeLeaves gives the subtrees at level -1:

https://wolfram.com/xid/0i1mnkxwu-4k9bjm

https://wolfram.com/xid/0i1mnkxwu-4gdab5

Possible Issues (1)Common pitfalls and unexpected behavior
TreeLevel by default starts at level 1, so does not extract the whole tree:

https://wolfram.com/xid/0i1mnkxwu-vi9w5r


https://wolfram.com/xid/0i1mnkxwu-hgamyh

Wolfram Research (2021), TreeLevel, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeLevel.html (updated 2022).
Text
Wolfram Research (2021), TreeLevel, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeLevel.html (updated 2022).
Wolfram Research (2021), TreeLevel, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeLevel.html (updated 2022).
CMS
Wolfram Language. 2021. "TreeLevel." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/TreeLevel.html.
Wolfram Language. 2021. "TreeLevel." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/TreeLevel.html.
APA
Wolfram Language. (2021). TreeLevel. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeLevel.html
Wolfram Language. (2021). TreeLevel. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeLevel.html
BibTeX
@misc{reference.wolfram_2025_treelevel, author="Wolfram Research", title="{TreeLevel}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TreeLevel.html}", note=[Accessed: 28-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_treelevel, organization={Wolfram Research}, title={TreeLevel}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeLevel.html}, note=[Accessed: 28-May-2025
]}