1719. Number Of Ways To Reconstruct A Tree
https://leetcode.com/problems/number-of-ways-to-reconstruct-a-tree
Description
You are given an array pairs, where pairs[i] = [xi, yi], and:
There are no duplicates.
xi < yi
Let ways be the number of rooted trees that satisfy the following conditions:
The tree consists of nodes whose values appeared in
pairs.A pair
[xi, yi]exists inpairsif and only ifxiis an ancestor ofyioryiis an ancestor ofxi.Note: the tree does not have to be a binary tree.
Two ways are considered to be different if there is at least one node that has different parents in both ways.
Return:
0ifways == 01ifways == 12ifways > 1
A rooted tree is a tree that has a single root node, and all edges are oriented to be outgoing from the root.
An ancestor of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.
Example 1:

Example 2:

Example 3:
Constraints:
1 <= pairs.length <= 1051 <= xi < yi <= 500The elements in
pairsare unique.
ac
Last updated
Was this helpful?