HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Can the same node appear twice in a tree?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
canthesametwicenodeappeartree

Problem

Can the same node appear twice in a tree?

I'm asking about the node object itself, not the node's value. For example, in the following code, a's left and right are the same node, b.

a = new TreeNode(1);
b = new TreeNode(2);
a.left = b; 
a.right = b;


Is this valid? Why or why not?

Solution

A tree is defined to be a set of nodes, with a parent-child relationship that satisfies certain properties. Thus, it doesn't make sense to ask whether a node can "appear" twice.

In your code snippet, you have constructed a DAG, not a tree.

Context

StackExchange Computer Science Q#121135, answer score: 7

Revisions (0)

No revisions yet.