Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 89 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online! Try it online!

This program output true for n=0. 1 more byte is required to convert it into 1.

f=(
 n, // nodes: `2*n` is number of nodes remaining
 s=0, // slots: how many nodes with 1 child currently
 l=1 // leaves: how many leaf nodes currently
)=>
 n? // is all nodes placed on the tree?
 s*f(n-=.5,s-1,l+1)+ // chose a non-leaf node, place it as an child
 l*f(n,s+1,l): // chose a leaf node, place it as an child
 !s // make sure all non-leaf nodes have 2 children

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

f=(
 n, // nodes: `2*n` is number of nodes remaining
 s=0, // slots: how many nodes with 1 child currently
 l=1 // leaves: how many leaf nodes currently
)=>
 n? // is all nodes placed on the tree?
 s*f(n-=.5,s-1,l+1)+ // chose a non-leaf node, place it as an child
 l*f(n,s+1,l): // chose a leaf node, place it as an child
 !s // make sure all non-leaf nodes have 2 children

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

This program output true for n=0. 1 more byte is required to convert it into 1.

f=(
 n, // nodes: `2*n` is number of nodes remaining
 s=0, // slots: how many nodes with 1 child currently
 l=1 // leaves: how many leaf nodes currently
)=>
 n? // is all nodes placed on the tree?
 s*f(n-=.5,s-1,l+1)+ // chose a non-leaf node, place it as an child
 l*f(n,s+1,l): // chose a leaf node, place it as an child
 !s // make sure all non-leaf nodes have 2 children
added 426 characters in body
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

f=(
 n, // nodes: `2*n` is number of nodes remaining
 s=0, // slots: how many nodes with 1 child currently
 l=1 // leaves: how many leaf nodes currently
)=>
 n? // is all nodes placed on the tree?
 s*f(n-=.5,s-1,l+1)+ // chose a non-leaf node, place it as an child
 l*f(n,s+1,l): // chose a leaf node, place it as an child
 !s // make sure all non-leaf nodes have 2 children

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

f=(
 n, // nodes: `2*n` is number of nodes remaining
 s=0, // slots: how many nodes with 1 child currently
 l=1 // leaves: how many leaf nodes currently
)=>
 n? // is all nodes placed on the tree?
 s*f(n-=.5,s-1,l+1)+ // chose a non-leaf node, place it as an child
 l*f(n,s+1,l): // chose a leaf node, place it as an child
 !s // make sure all non-leaf nodes have 2 children
Source Link
tsh
  • 36.2k
  • 2
  • 36
  • 133

JavaScript (Node.js), 51 bytes

f=(n,s=0,l=1)=>n?s*f(n-=.5,s-1,l+1)+l*f(n,s+1,l):!s

Try it online!

AltStyle によって変換されたページ (->オリジナル) /