Write The Shortest Program to Calculate Height of a Binary TreeTips for golfing in CFree a Binary TreePrint a Binary TreeEnumerate all binary trees with n nodesFind a binary tree's deepest nodeConvert binary search tree into ordered linked listPrint a non-clashing binary search treeTree traversingCompute the height of a radix treeRooting for Trees With the Right NodesBinary tree rotations
Is an "are" omitted in this sentence
HackerRank Implement Queue using two stacks Solution
How were x-ray diffraction patterns deciphered before computers?
Lower bound for the number of lattice points on high dimensional spheres
What does Argus Filch specifically do?
Why do my fried eggs start browning very fast?
Why did the United States not resort to nuclear weapons in Vietnam?
What is the reason behind water not falling from a bucket at the top of loop?
Being told my "network" isn't PCI compliant. I don't even have a server! Do I have to comply?
Skipping same old introductions
Export economy of Mars
Unlocked Package Dependencies
In MTG, was there ever a five-color deck that worked well?
(7 of 11: Fillomino) What is Pyramid Cult's Favorite Shape?
Does a bard know when a character uses their Bardic Inspiration?
Does WSL2 runs Linux in a virtual machine or alongside windows Kernel?
How does Rust's 128-bit integer `i128` work on a 64-bit system?
Partial Fractions: Why does this shortcut method work?
Can an unintentional murderer leave Ir Miklat for Shalosh Regalim?
Can there be multiple energy eigenstates corresponding to the same eigenvalue of a Hamiltonian (Pauli-X)?
Phase portrait of a system of differential equations
Why are sugars in whole fruits not digested the same way sugars in juice are?
Can I say "Gesundheit" if someone is coughing?
Is there any difference between "result in" and "end up with"?
Write The Shortest Program to Calculate Height of a Binary Tree
Tips for golfing in CFree a Binary TreePrint a Binary TreeEnumerate all binary trees with n nodesFind a binary tree's deepest nodeConvert binary search tree into ordered linked listPrint a non-clashing binary search treeTree traversingCompute the height of a radix treeRooting for Trees With the Right NodesBinary tree rotations
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
The height of a binary tree is the distance from the root node to the node child that is farthest from the root.
Below is an example:
2 <-- root: Height 1
/
7 5 <-- Height 2
/
2 6 9 <-- Height 3
/ /
5 11 4 <-- Height 4
Height of binary tree: 4
Definition of a Binary Tree
A tree is an object that contains a signed integer value and either two other trees or pointers to them.
The structure of the binary tree struct looks something like the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
The challenge:
Input
The root of a binary tree
Output
The number that represents the height of a binary tree
Assuming you are given the root of a binary tree as input, write the shortest program that calculates the height of a binary tree and returns the height. The program with least amount of bytes (accounting whitespaces) wins.
code-golf binary-tree
New contributor
$endgroup$
|
show 7 more comments
$begingroup$
The height of a binary tree is the distance from the root node to the node child that is farthest from the root.
Below is an example:
2 <-- root: Height 1
/
7 5 <-- Height 2
/
2 6 9 <-- Height 3
/ /
5 11 4 <-- Height 4
Height of binary tree: 4
Definition of a Binary Tree
A tree is an object that contains a signed integer value and either two other trees or pointers to them.
The structure of the binary tree struct looks something like the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
The challenge:
Input
The root of a binary tree
Output
The number that represents the height of a binary tree
Assuming you are given the root of a binary tree as input, write the shortest program that calculates the height of a binary tree and returns the height. The program with least amount of bytes (accounting whitespaces) wins.
code-golf binary-tree
New contributor
$endgroup$
2
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
3
$begingroup$
...but then my tree object could just have a property, sayh
. Might be better to define a specific structure made just of lists for the purpose of this challenge.
$endgroup$
– Jonathan Allan
8 hours ago
4
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
1
$begingroup$
So, is a valid representation a list of length 3[root_value, left_node, right_node]
where each ofleft_node
andright_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition likea tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.
$endgroup$
– Jo King
3 hours ago
|
show 7 more comments
$begingroup$
The height of a binary tree is the distance from the root node to the node child that is farthest from the root.
Below is an example:
2 <-- root: Height 1
/
7 5 <-- Height 2
/
2 6 9 <-- Height 3
/ /
5 11 4 <-- Height 4
Height of binary tree: 4
Definition of a Binary Tree
A tree is an object that contains a signed integer value and either two other trees or pointers to them.
The structure of the binary tree struct looks something like the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
The challenge:
Input
The root of a binary tree
Output
The number that represents the height of a binary tree
Assuming you are given the root of a binary tree as input, write the shortest program that calculates the height of a binary tree and returns the height. The program with least amount of bytes (accounting whitespaces) wins.
code-golf binary-tree
New contributor
$endgroup$
The height of a binary tree is the distance from the root node to the node child that is farthest from the root.
Below is an example:
2 <-- root: Height 1
/
7 5 <-- Height 2
/
2 6 9 <-- Height 3
/ /
5 11 4 <-- Height 4
Height of binary tree: 4
Definition of a Binary Tree
A tree is an object that contains a signed integer value and either two other trees or pointers to them.
The structure of the binary tree struct looks something like the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
The challenge:
Input
The root of a binary tree
Output
The number that represents the height of a binary tree
Assuming you are given the root of a binary tree as input, write the shortest program that calculates the height of a binary tree and returns the height. The program with least amount of bytes (accounting whitespaces) wins.
code-golf binary-tree
code-golf binary-tree
New contributor
New contributor
edited 3 hours ago
T. Salim
New contributor
asked 8 hours ago
T. SalimT. Salim
476 bronze badges
476 bronze badges
New contributor
New contributor
2
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
3
$begingroup$
...but then my tree object could just have a property, sayh
. Might be better to define a specific structure made just of lists for the purpose of this challenge.
$endgroup$
– Jonathan Allan
8 hours ago
4
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
1
$begingroup$
So, is a valid representation a list of length 3[root_value, left_node, right_node]
where each ofleft_node
andright_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition likea tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.
$endgroup$
– Jo King
3 hours ago
|
show 7 more comments
2
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
3
$begingroup$
...but then my tree object could just have a property, sayh
. Might be better to define a specific structure made just of lists for the purpose of this challenge.
$endgroup$
– Jonathan Allan
8 hours ago
4
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
1
$begingroup$
So, is a valid representation a list of length 3[root_value, left_node, right_node]
where each ofleft_node
andright_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition likea tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.
$endgroup$
– Jo King
3 hours ago
2
2
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
3
3
$begingroup$
...but then my tree object could just have a property, say
h
. Might be better to define a specific structure made just of lists for the purpose of this challenge.$endgroup$
– Jonathan Allan
8 hours ago
$begingroup$
...but then my tree object could just have a property, say
h
. Might be better to define a specific structure made just of lists for the purpose of this challenge.$endgroup$
– Jonathan Allan
8 hours ago
4
4
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
1
1
$begingroup$
So, is a valid representation a list of length 3
[root_value, left_node, right_node]
where each of left_node
and right_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
So, is a valid representation a list of length 3
[root_value, left_node, right_node]
where each of left_node
and right_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.$endgroup$
– Jonathan Allan
7 hours ago
2
2
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition like
a tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.$endgroup$
– Jo King
3 hours ago
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition like
a tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.$endgroup$
– Jo King
3 hours ago
|
show 7 more comments
9 Answers
9
active
oldest
votes
$begingroup$
Jelly, 3 bytes
ŒḊ’
A monadic Link accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which yields the height.
Try it online!
How?
Pretty trivial in Jelly:
ŒḊ’ - Link: list, as described above
ŒḊ - depth
’ - decremented (since leaves are `[value, [], []]`)
$endgroup$
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
I wonder how controversial it would be to represent a leaf asx
instead of[x, [], []]
...
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
add a comment |
$begingroup$
Python 2, 35 33 bytes
Thanks to Arnauld for noicing an oversight and saving 4.
f=lambda a:a>[]and-~max(map(f,a))
A recursive function accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which returns the height.
Try it online!
Note that []
will return False
, but in Python False==0
.
$endgroup$
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 35 33 bytes
Input structure: [[left_node], [right_node], value]
f=([a,b])=>a?1+f(f(a)>f(b)?a:b):0
Try it online!
Commented
f = // f is a recursive function taking
([a, b]) => // a node of the tree split into
// a[] = left child, b[] = right child (the value is ignored)
a ? // if a[] is defined:
1 + // increment the final result for this branch
f( // and add:
f(a) > f(b) ? a : b // f(a) if f(a) > f(b) or f(b) otherwise
) //
: // else:
0 // stop recursion and return 0
$endgroup$
$begingroup$
Looks like you can save a byte witha&&-~
.
$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
add a comment |
$begingroup$
Haskell, 33 bytes
h L=0
h(N l r _)=1+max(h l)(h r)
Using the custom tree type data T = L | N T T Int
, which is the Haskell equivalent of the C struct given in the challenge.
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 30 bytes
+$_&&1+max map &?BLOCK,.[^2]
Try it online!
Input is a 3-element list (l, r, v)
.
$endgroup$
$begingroup$
The&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!
$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like$!
or$/
feels like cheating to me.
$endgroup$
– nwellnhof
3 hours ago
add a comment |
$begingroup$
C, 43 bytes
h(T*r)r=r?1+(int)fmax(h(r->l),h(r->r)):0;
Structure of binary tree is the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
New contributor
$endgroup$
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
add a comment |
$begingroup$
Scheme, 72 Bytes
(define(f h)(if(null? h)0(+ 1(max(f(car(cdr h)))(f(car(cdr(cdr h))))))))
More Readable Version:
(define (f h)
(if (null? h)
0
(+ 1
(max
(f (car (cdr h)))
(f (car (cdr (cdr h))))
)
)
)
)
Using lists of the form (data, left, right) to represent a tree. E.g.
1
/
2 3
/
4 5
is represented as: (1 (2 (4 () ()) (5 () ())) (3 () ())
(1
(2
(4 () ())
``` (5 () ())
(3 () ())
)
Try it Online!
$endgroup$
add a comment |
$begingroup$
Charcoal, 29 bytes
⊞θ⁰⊞υθFυ«≔⊕⊟ιθFΦι∧κλ⊞υ⊞Oκθ»Iθ
Try it online! Link is to verbose version of code. Temporarily modifies the tree during processing. Explanation:
⊞θ⁰
Push zero to the root node.
⊞υθ
Push the root node to the list of all nodes.
Fυ«
Perform a breadth-first search of the tree.
≔⊕⊟ιθ
Get the depth of this node.
FΦι∧κλ
Loop over any child nodes.
⊞υ⊞Oκθ
Tell the child node its parent's depth and push it to the list of all nodes.
»Iθ
Once all the nodes have been traversed print the depth of the last node. Since the traversal was breadth-first, this will be the height of the tree.
$endgroup$
add a comment |
$begingroup$
Japt, 9 bytes
Ω¡ÒßXÃrw
Try it
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
T. Salim is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f189220%2fwrite-the-shortest-program-to-calculate-height-of-a-binary-tree%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 3 bytes
ŒḊ’
A monadic Link accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which yields the height.
Try it online!
How?
Pretty trivial in Jelly:
ŒḊ’ - Link: list, as described above
ŒḊ - depth
’ - decremented (since leaves are `[value, [], []]`)
$endgroup$
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
I wonder how controversial it would be to represent a leaf asx
instead of[x, [], []]
...
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
add a comment |
$begingroup$
Jelly, 3 bytes
ŒḊ’
A monadic Link accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which yields the height.
Try it online!
How?
Pretty trivial in Jelly:
ŒḊ’ - Link: list, as described above
ŒḊ - depth
’ - decremented (since leaves are `[value, [], []]`)
$endgroup$
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
I wonder how controversial it would be to represent a leaf asx
instead of[x, [], []]
...
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
add a comment |
$begingroup$
Jelly, 3 bytes
ŒḊ’
A monadic Link accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which yields the height.
Try it online!
How?
Pretty trivial in Jelly:
ŒḊ’ - Link: list, as described above
ŒḊ - depth
’ - decremented (since leaves are `[value, [], []]`)
$endgroup$
Jelly, 3 bytes
ŒḊ’
A monadic Link accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which yields the height.
Try it online!
How?
Pretty trivial in Jelly:
ŒḊ’ - Link: list, as described above
ŒḊ - depth
’ - decremented (since leaves are `[value, [], []]`)
edited 7 hours ago
answered 7 hours ago
Jonathan AllanJonathan Allan
58.2k5 gold badges43 silver badges183 bronze badges
58.2k5 gold badges43 silver badges183 bronze badges
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
I wonder how controversial it would be to represent a leaf asx
instead of[x, [], []]
...
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
add a comment |
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
I wonder how controversial it would be to represent a leaf asx
instead of[x, [], []]
...
$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
$begingroup$
Jonathon Allen, that is an interesting language you are using. As a newcomer, may you provide a link or a website referral that tutors people how to use Jelly?
$endgroup$
– T. Salim
7 hours ago
1
1
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Click the link in the header - it's a golfing Language developed by Dennis, one of the site's moderators.
$endgroup$
– Jonathan Allan
7 hours ago
2
2
$begingroup$
I wonder how controversial it would be to represent a leaf as
x
instead of [x, [], []]
...$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
I wonder how controversial it would be to represent a leaf as
x
instead of [x, [], []]
...$endgroup$
– Erik the Outgolfer
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@EriktheOutgolfer To keep with the "pointer" and "struct" nature of the question I think every node should be of the same form.
$endgroup$
– Jonathan Allan
7 hours ago
add a comment |
$begingroup$
Python 2, 35 33 bytes
Thanks to Arnauld for noicing an oversight and saving 4.
f=lambda a:a>[]and-~max(map(f,a))
A recursive function accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which returns the height.
Try it online!
Note that []
will return False
, but in Python False==0
.
$endgroup$
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
add a comment |
$begingroup$
Python 2, 35 33 bytes
Thanks to Arnauld for noicing an oversight and saving 4.
f=lambda a:a>[]and-~max(map(f,a))
A recursive function accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which returns the height.
Try it online!
Note that []
will return False
, but in Python False==0
.
$endgroup$
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
add a comment |
$begingroup$
Python 2, 35 33 bytes
Thanks to Arnauld for noicing an oversight and saving 4.
f=lambda a:a>[]and-~max(map(f,a))
A recursive function accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which returns the height.
Try it online!
Note that []
will return False
, but in Python False==0
.
$endgroup$
Python 2, 35 33 bytes
Thanks to Arnauld for noicing an oversight and saving 4.
f=lambda a:a>[]and-~max(map(f,a))
A recursive function accepting a list representing the tree: [root_value, left_tree, right_tree]
, where each of left_tree
and right_tree
are similar structures (empty if need be), which returns the height.
Try it online!
Note that []
will return False
, but in Python False==0
.
edited 6 hours ago
answered 7 hours ago
Jonathan AllanJonathan Allan
58.2k5 gold badges43 silver badges183 bronze badges
58.2k5 gold badges43 silver badges183 bronze badges
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
add a comment |
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
$begingroup$
The same person is allowed to give two different answers to the same question?
$endgroup$
– T. Salim
7 hours ago
2
2
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
Yeah, of course, golf is a competition at a language level. Even a second entry in the same language is sometimes acceptable, if the approach is very different.
$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@Arnauld Guess so (I'd assumed non-integers might be present for some reason)
$endgroup$
– Jonathan Allan
6 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 35 33 bytes
Input structure: [[left_node], [right_node], value]
f=([a,b])=>a?1+f(f(a)>f(b)?a:b):0
Try it online!
Commented
f = // f is a recursive function taking
([a, b]) => // a node of the tree split into
// a[] = left child, b[] = right child (the value is ignored)
a ? // if a[] is defined:
1 + // increment the final result for this branch
f( // and add:
f(a) > f(b) ? a : b // f(a) if f(a) > f(b) or f(b) otherwise
) //
: // else:
0 // stop recursion and return 0
$endgroup$
$begingroup$
Looks like you can save a byte witha&&-~
.
$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 35 33 bytes
Input structure: [[left_node], [right_node], value]
f=([a,b])=>a?1+f(f(a)>f(b)?a:b):0
Try it online!
Commented
f = // f is a recursive function taking
([a, b]) => // a node of the tree split into
// a[] = left child, b[] = right child (the value is ignored)
a ? // if a[] is defined:
1 + // increment the final result for this branch
f( // and add:
f(a) > f(b) ? a : b // f(a) if f(a) > f(b) or f(b) otherwise
) //
: // else:
0 // stop recursion and return 0
$endgroup$
$begingroup$
Looks like you can save a byte witha&&-~
.
$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 35 33 bytes
Input structure: [[left_node], [right_node], value]
f=([a,b])=>a?1+f(f(a)>f(b)?a:b):0
Try it online!
Commented
f = // f is a recursive function taking
([a, b]) => // a node of the tree split into
// a[] = left child, b[] = right child (the value is ignored)
a ? // if a[] is defined:
1 + // increment the final result for this branch
f( // and add:
f(a) > f(b) ? a : b // f(a) if f(a) > f(b) or f(b) otherwise
) //
: // else:
0 // stop recursion and return 0
$endgroup$
JavaScript (ES6), 35 33 bytes
Input structure: [[left_node], [right_node], value]
f=([a,b])=>a?1+f(f(a)>f(b)?a:b):0
Try it online!
Commented
f = // f is a recursive function taking
([a, b]) => // a node of the tree split into
// a[] = left child, b[] = right child (the value is ignored)
a ? // if a[] is defined:
1 + // increment the final result for this branch
f( // and add:
f(a) > f(b) ? a : b // f(a) if f(a) > f(b) or f(b) otherwise
) //
: // else:
0 // stop recursion and return 0
edited 6 hours ago
answered 7 hours ago
ArnauldArnauld
89.2k7 gold badges103 silver badges365 bronze badges
89.2k7 gold badges103 silver badges365 bronze badges
$begingroup$
Looks like you can save a byte witha&&-~
.
$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
add a comment |
$begingroup$
Looks like you can save a byte witha&&-~
.
$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
$begingroup$
Looks like you can save a byte with
a&&-~
.$endgroup$
– Shaggy
2 hours ago
$begingroup$
Looks like you can save a byte with
a&&-~
.$endgroup$
– Shaggy
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
$begingroup$
@Shaggy That would lead to comparisons with undefined.
$endgroup$
– Arnauld
2 hours ago
add a comment |
$begingroup$
Haskell, 33 bytes
h L=0
h(N l r _)=1+max(h l)(h r)
Using the custom tree type data T = L | N T T Int
, which is the Haskell equivalent of the C struct given in the challenge.
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 33 bytes
h L=0
h(N l r _)=1+max(h l)(h r)
Using the custom tree type data T = L | N T T Int
, which is the Haskell equivalent of the C struct given in the challenge.
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 33 bytes
h L=0
h(N l r _)=1+max(h l)(h r)
Using the custom tree type data T = L | N T T Int
, which is the Haskell equivalent of the C struct given in the challenge.
Try it online!
$endgroup$
Haskell, 33 bytes
h L=0
h(N l r _)=1+max(h l)(h r)
Using the custom tree type data T = L | N T T Int
, which is the Haskell equivalent of the C struct given in the challenge.
Try it online!
answered 5 hours ago
niminimi
33.7k3 gold badges27 silver badges91 bronze badges
33.7k3 gold badges27 silver badges91 bronze badges
add a comment |
add a comment |
$begingroup$
Perl 6, 30 bytes
+$_&&1+max map &?BLOCK,.[^2]
Try it online!
Input is a 3-element list (l, r, v)
.
$endgroup$
$begingroup$
The&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!
$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like$!
or$/
feels like cheating to me.
$endgroup$
– nwellnhof
3 hours ago
add a comment |
$begingroup$
Perl 6, 30 bytes
+$_&&1+max map &?BLOCK,.[^2]
Try it online!
Input is a 3-element list (l, r, v)
.
$endgroup$
$begingroup$
The&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!
$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like$!
or$/
feels like cheating to me.
$endgroup$
– nwellnhof
3 hours ago
add a comment |
$begingroup$
Perl 6, 30 bytes
+$_&&1+max map &?BLOCK,.[^2]
Try it online!
Input is a 3-element list (l, r, v)
.
$endgroup$
Perl 6, 30 bytes
+$_&&1+max map &?BLOCK,.[^2]
Try it online!
Input is a 3-element list (l, r, v)
.
answered 4 hours ago
nwellnhofnwellnhof
7,8901 gold badge12 silver badges29 bronze badges
7,8901 gold badge12 silver badges29 bronze badges
$begingroup$
The&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!
$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like$!
or$/
feels like cheating to me.
$endgroup$
– nwellnhof
3 hours ago
add a comment |
$begingroup$
The&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!
$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like$!
or$/
feels like cheating to me.
$endgroup$
– nwellnhof
3 hours ago
$begingroup$
The
&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!$endgroup$
– Jo King
3 hours ago
$begingroup$
The
&?BLOCK
trick is interesting but it's a couple of bytes shorter to assign the block to $!$endgroup$
– Jo King
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like
$!
or $/
feels like cheating to me.$endgroup$
– nwellnhof
3 hours ago
$begingroup$
@JoKing I don't know. Storing the challenge solution in a volatile global like
$!
or $/
feels like cheating to me.$endgroup$
– nwellnhof
3 hours ago
add a comment |
$begingroup$
C, 43 bytes
h(T*r)r=r?1+(int)fmax(h(r->l),h(r->r)):0;
Structure of binary tree is the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
New contributor
$endgroup$
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
add a comment |
$begingroup$
C, 43 bytes
h(T*r)r=r?1+(int)fmax(h(r->l),h(r->r)):0;
Structure of binary tree is the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
New contributor
$endgroup$
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
add a comment |
$begingroup$
C, 43 bytes
h(T*r)r=r?1+(int)fmax(h(r->l),h(r->r)):0;
Structure of binary tree is the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
New contributor
$endgroup$
C, 43 bytes
h(T*r)r=r?1+(int)fmax(h(r->l),h(r->r)):0;
Structure of binary tree is the following:
typedef struct tree
struct tree * l;
struct tree * r;
int v;
tree;
New contributor
edited 4 hours ago
New contributor
answered 7 hours ago
T. SalimT. Salim
476 bronze badges
476 bronze badges
New contributor
New contributor
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
add a comment |
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
1
1
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
$begingroup$
55 bytes Try it online! Some C-specific golfing tricks are here!
$endgroup$
– ErikF
5 hours ago
1
1
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
@ErikF Or 45 bytes
$endgroup$
– Arnauld
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
$begingroup$
Thanks ErikF and Arnauld. I should try out the ternary operator more often instead of if-else-if chains.
$endgroup$
– T. Salim
4 hours ago
2
2
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
$begingroup$
43 bytes
$endgroup$
– nwellnhof
4 hours ago
2
2
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
$begingroup$
If your submission relies on flags, can you please add them to the header of your submission?
$endgroup$
– Jo King
3 hours ago
add a comment |
$begingroup$
Scheme, 72 Bytes
(define(f h)(if(null? h)0(+ 1(max(f(car(cdr h)))(f(car(cdr(cdr h))))))))
More Readable Version:
(define (f h)
(if (null? h)
0
(+ 1
(max
(f (car (cdr h)))
(f (car (cdr (cdr h))))
)
)
)
)
Using lists of the form (data, left, right) to represent a tree. E.g.
1
/
2 3
/
4 5
is represented as: (1 (2 (4 () ()) (5 () ())) (3 () ())
(1
(2
(4 () ())
``` (5 () ())
(3 () ())
)
Try it Online!
$endgroup$
add a comment |
$begingroup$
Scheme, 72 Bytes
(define(f h)(if(null? h)0(+ 1(max(f(car(cdr h)))(f(car(cdr(cdr h))))))))
More Readable Version:
(define (f h)
(if (null? h)
0
(+ 1
(max
(f (car (cdr h)))
(f (car (cdr (cdr h))))
)
)
)
)
Using lists of the form (data, left, right) to represent a tree. E.g.
1
/
2 3
/
4 5
is represented as: (1 (2 (4 () ()) (5 () ())) (3 () ())
(1
(2
(4 () ())
``` (5 () ())
(3 () ())
)
Try it Online!
$endgroup$
add a comment |
$begingroup$
Scheme, 72 Bytes
(define(f h)(if(null? h)0(+ 1(max(f(car(cdr h)))(f(car(cdr(cdr h))))))))
More Readable Version:
(define (f h)
(if (null? h)
0
(+ 1
(max
(f (car (cdr h)))
(f (car (cdr (cdr h))))
)
)
)
)
Using lists of the form (data, left, right) to represent a tree. E.g.
1
/
2 3
/
4 5
is represented as: (1 (2 (4 () ()) (5 () ())) (3 () ())
(1
(2
(4 () ())
``` (5 () ())
(3 () ())
)
Try it Online!
$endgroup$
Scheme, 72 Bytes
(define(f h)(if(null? h)0(+ 1(max(f(car(cdr h)))(f(car(cdr(cdr h))))))))
More Readable Version:
(define (f h)
(if (null? h)
0
(+ 1
(max
(f (car (cdr h)))
(f (car (cdr (cdr h))))
)
)
)
)
Using lists of the form (data, left, right) to represent a tree. E.g.
1
/
2 3
/
4 5
is represented as: (1 (2 (4 () ()) (5 () ())) (3 () ())
(1
(2
(4 () ())
``` (5 () ())
(3 () ())
)
Try it Online!
edited 4 hours ago
answered 4 hours ago
Zachary CottonZachary Cotton
4992 silver badges5 bronze badges
4992 silver badges5 bronze badges
add a comment |
add a comment |
$begingroup$
Charcoal, 29 bytes
⊞θ⁰⊞υθFυ«≔⊕⊟ιθFΦι∧κλ⊞υ⊞Oκθ»Iθ
Try it online! Link is to verbose version of code. Temporarily modifies the tree during processing. Explanation:
⊞θ⁰
Push zero to the root node.
⊞υθ
Push the root node to the list of all nodes.
Fυ«
Perform a breadth-first search of the tree.
≔⊕⊟ιθ
Get the depth of this node.
FΦι∧κλ
Loop over any child nodes.
⊞υ⊞Oκθ
Tell the child node its parent's depth and push it to the list of all nodes.
»Iθ
Once all the nodes have been traversed print the depth of the last node. Since the traversal was breadth-first, this will be the height of the tree.
$endgroup$
add a comment |
$begingroup$
Charcoal, 29 bytes
⊞θ⁰⊞υθFυ«≔⊕⊟ιθFΦι∧κλ⊞υ⊞Oκθ»Iθ
Try it online! Link is to verbose version of code. Temporarily modifies the tree during processing. Explanation:
⊞θ⁰
Push zero to the root node.
⊞υθ
Push the root node to the list of all nodes.
Fυ«
Perform a breadth-first search of the tree.
≔⊕⊟ιθ
Get the depth of this node.
FΦι∧κλ
Loop over any child nodes.
⊞υ⊞Oκθ
Tell the child node its parent's depth and push it to the list of all nodes.
»Iθ
Once all the nodes have been traversed print the depth of the last node. Since the traversal was breadth-first, this will be the height of the tree.
$endgroup$
add a comment |
$begingroup$
Charcoal, 29 bytes
⊞θ⁰⊞υθFυ«≔⊕⊟ιθFΦι∧κλ⊞υ⊞Oκθ»Iθ
Try it online! Link is to verbose version of code. Temporarily modifies the tree during processing. Explanation:
⊞θ⁰
Push zero to the root node.
⊞υθ
Push the root node to the list of all nodes.
Fυ«
Perform a breadth-first search of the tree.
≔⊕⊟ιθ
Get the depth of this node.
FΦι∧κλ
Loop over any child nodes.
⊞υ⊞Oκθ
Tell the child node its parent's depth and push it to the list of all nodes.
»Iθ
Once all the nodes have been traversed print the depth of the last node. Since the traversal was breadth-first, this will be the height of the tree.
$endgroup$
Charcoal, 29 bytes
⊞θ⁰⊞υθFυ«≔⊕⊟ιθFΦι∧κλ⊞υ⊞Oκθ»Iθ
Try it online! Link is to verbose version of code. Temporarily modifies the tree during processing. Explanation:
⊞θ⁰
Push zero to the root node.
⊞υθ
Push the root node to the list of all nodes.
Fυ«
Perform a breadth-first search of the tree.
≔⊕⊟ιθ
Get the depth of this node.
FΦι∧κλ
Loop over any child nodes.
⊞υ⊞Oκθ
Tell the child node its parent's depth and push it to the list of all nodes.
»Iθ
Once all the nodes have been traversed print the depth of the last node. Since the traversal was breadth-first, this will be the height of the tree.
answered 3 hours ago
NeilNeil
86.7k8 gold badges46 silver badges183 bronze badges
86.7k8 gold badges46 silver badges183 bronze badges
add a comment |
add a comment |
$begingroup$
Japt, 9 bytes
Ω¡ÒßXÃrw
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 9 bytes
Ω¡ÒßXÃrw
Try it
$endgroup$
add a comment |
$begingroup$
Japt, 9 bytes
Ω¡ÒßXÃrw
Try it
$endgroup$
Japt, 9 bytes
Ω¡ÒßXÃrw
Try it
answered 3 hours ago
ShaggyShaggy
20.7k3 gold badges20 silver badges69 bronze badges
20.7k3 gold badges20 silver badges69 bronze badges
add a comment |
add a comment |
T. Salim is a new contributor. Be nice, and check out our Code of Conduct.
T. Salim is a new contributor. Be nice, and check out our Code of Conduct.
T. Salim is a new contributor. Be nice, and check out our Code of Conduct.
T. Salim is a new contributor. Be nice, and check out our Code of Conduct.
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f189220%2fwrite-the-shortest-program-to-calculate-height-of-a-binary-tree%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
What do languages without pointers take?
$endgroup$
– Jonathan Allan
8 hours ago
3
$begingroup$
...but then my tree object could just have a property, say
h
. Might be better to define a specific structure made just of lists for the purpose of this challenge.$endgroup$
– Jonathan Allan
8 hours ago
4
$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
7 hours ago
1
$begingroup$
So, is a valid representation a list of length 3
[root_value, left_node, right_node]
where each ofleft_node
andright_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others.$endgroup$
– Jonathan Allan
7 hours ago
2
$begingroup$
Can you edit the question to include what constitutes a valid binary structure? Perhaps a definition like
a tree is an object that contains a value and either two other trees or pointers to them
. A definition that is inclusive of languages without objects would also be nice too.$endgroup$
– Jo King
3 hours ago