Skip to content

Number of nodes (or equivalent) under a node #2525

Answered by jeromekelleher
hyanwong asked this question in Q&A
Discussion options

You must be logged in to vote

You can make the array in a single pass through the postorder array, right?

total_nodes = np.zeros_like(tree.postorder)
for u in tree.postorder:
     for v in tree.children(u):
           total_nodes[u] += total_nodes[v]
     total_nodes[u] += 1

Or such like?

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@hyanwong
Comment options

hyanwong Nov 17, 2022
Maintainer Author

Answer selected by hyanwong
Comment options

You must be logged in to vote
1 reply
@hyanwong
Comment options

hyanwong Sep 29, 2022
Maintainer Author

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants