3

Can a tree node be both a root node and leaf node? If so, why?

I understand that the root node is at Level 0 of the tree. I also understand that a leaf node is a tree node that has no children. So if a tree has a root node A and nothing else, would that root node A also be considered a leaf node?

Is it possible for a tree node to be both a root node and leaf node?

trincot
  • 317,000
  • 35
  • 244
  • 286
pyuntae
  • 742
  • 3
  • 10
  • 25

3 Answers3

5

From this Wikipedia link on Tree Data Structures, section Terminologies used in Trees:

  • Root – The top node in a tree.
  • Leaf – A node with no children.

    So by definition, if a Tree structure only consists of a single Node, that single Node will in fact be both a Root and a Leaf. Read the link for further detail.

    Now, whether your professor considers this to be correct is a whole 'nother story.

  • robotlos
    • 536
    • 2
    • 10
    3

    If a tree node can have both a root node and leaf node, why?

    You answered your own question:

    I understand that the root node is at Level 0 of the tree. I also understand that a leaf node is a tree node that has no children.

    Here's a diagram for better understanding:

      root
       |
    tree node
       |
    leaf node
    

    So if a tree node had a root node of A and nothing else, would that be considered a leaf node also?

    Refer to your definition directly above. Diagram for better understanding:

    A - root node
        |
     tree node   <-- tree node & leaf node
        |
      (null)
    

    It is possible and how is it possible for a tree node to be both a root node and leaf node?

    If you consider the root node to be a tree node, then a root node with no children will be both a tree node and leaf node.

    Jonny Henly
    • 4,023
    • 4
    • 26
    • 43
    1

    If a tree node can have both a root node and leaf node, why

    If a node is somewhere in between the root and a leaf, it has both a root node and a leaf under it.

    how is it possible for a tree node to be both a root node and leaf node?

    If the tree only consists of one node, that node is both the root and a leaf.

    f1sh
    • 11,489
    • 3
    • 25
    • 51