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

Speaker impedance: rewiring four 8 Ω speakers for use with 8 Ω amp output

How was the cosmonaut of the Soviet moon mission supposed to get back in the return vehicle?

Why have both: BJT and FET transistors on IC output?

How do people drown while wearing a life jacket?

How to construct BezierFunction for BezierCurve with npts>4 and SplineDegree -> 3?

How long should I wait to plug in my refrigerator after unplugging it?

Difference between "jail" and "prison" in German

Confused over role of 「自分が」in this particular passage

How to win an all out war against ants

Can you shove a friendly creature?

Using Forstner bits instead of hole saws

Phase portrait of a system of differential equations

C# TCP server/client class

Why does BezierFunction not follow BezierCurve at npts>4?

How to handle many times series?

Is Norway in the Single Market?

Being told my "network" isn't PCI compliant. I don't even have a server! Do I have to comply?

Does a bard know when a character uses their Bardic Inspiration?

Representation of the concatenation at the type level

Current in only inductive AC circuit

In MTG, was there ever a five-color deck that worked well?

Is it moral to remove/hide certain parts of a photo, as a photographer?

Feedback diagram

What is the reason behind water not falling from a bucket at the top of loop?



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;








4












$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.










share|improve this question









New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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, 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
    8 hours ago






  • 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
    8 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
    4 hours ago


















4












$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.










share|improve this question









New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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, 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
    8 hours ago






  • 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
    8 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
    4 hours ago














4












4








4


1



$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.










share|improve this question









New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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






share|improve this question









New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 3 hours ago







T. Salim













New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









T. SalimT. Salim

476 bronze badges




476 bronze badges




New contributor



T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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
    8 hours ago






  • 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
    8 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
    4 hours ago













  • 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
    8 hours ago






  • 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
    8 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
    4 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
8 hours ago




$begingroup$
@T.Salim In the future, please consider posting in the sandbox first.
$endgroup$
– wizzwizz4
8 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
8 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
8 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
4 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
4 hours ago











9 Answers
9






active

oldest

votes


















4












$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, [], []]`)





share|improve this answer











$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
    8 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
    8 hours ago







  • 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$
    @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



















3












$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.






share|improve this answer











$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
    7 hours ago



















2












$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





share|improve this answer











$endgroup$














  • $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



















1












$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!






share|improve this answer









$endgroup$






















    1












    $begingroup$


    Perl 6, 30 bytes





    +$_&&1+max map &?BLOCK,.[^2]


    Try it online!



    Input is a 3-element list (l, r, v).






    share|improve this answer









    $endgroup$














    • $begingroup$
      The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
      $endgroup$
      – Jo King
      4 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
      4 hours ago


















    1












    $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;





    share|improve this answer










    New contributor



    T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    $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
      5 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
      5 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


















    0












    $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!






    share|improve this answer











    $endgroup$






















      0












      $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.






      share|improve this answer









      $endgroup$






















        0












        $begingroup$


        Japt, 9 bytes



        Ω¡ÒßXÃrw


        Try it






        share|improve this answer









        $endgroup$

















          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.









          draft saved

          draft discarded


















          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









          4












          $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, [], []]`)





          share|improve this answer











          $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
            8 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
            8 hours ago







          • 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$
            @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
















          4












          $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, [], []]`)





          share|improve this answer











          $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
            8 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
            8 hours ago







          • 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$
            @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














          4












          4








          4





          $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, [], []]`)





          share|improve this answer











          $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, [], []]`)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 8 hours ago









          Jonathan AllanJonathan Allan

          58.3k5 gold badges43 silver badges183 bronze badges




          58.3k5 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
            8 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
            8 hours ago







          • 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$
            @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
            8 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
            8 hours ago







          • 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$
            @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
          8 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
          8 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
          8 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
          8 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














          3












          $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.






          share|improve this answer











          $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
            7 hours ago
















          3












          $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.






          share|improve this answer











          $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
            7 hours ago














          3












          3








          3





          $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.






          share|improve this answer











          $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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 7 hours ago









          Jonathan AllanJonathan Allan

          58.3k5 gold badges43 silver badges183 bronze badges




          58.3k5 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
            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




            $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
            7 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
          7 hours ago





          $begingroup$
          @Arnauld Guess so (I'd assumed non-integers might be present for some reason)
          $endgroup$
          – Jonathan Allan
          7 hours ago












          2












          $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





          share|improve this answer











          $endgroup$














          • $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
















          2












          $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





          share|improve this answer











          $endgroup$














          • $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














          2












          2








          2





          $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





          share|improve this answer











          $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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 with a&&-~.
            $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$
            @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












          1












          $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!






          share|improve this answer









          $endgroup$



















            1












            $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!






            share|improve this answer









            $endgroup$

















              1












              1








              1





              $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!






              share|improve this answer









              $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!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 5 hours ago









              niminimi

              33.7k3 gold badges27 silver badges91 bronze badges




              33.7k3 gold badges27 silver badges91 bronze badges
























                  1












                  $begingroup$


                  Perl 6, 30 bytes





                  +$_&&1+max map &?BLOCK,.[^2]


                  Try it online!



                  Input is a 3-element list (l, r, v).






                  share|improve this answer









                  $endgroup$














                  • $begingroup$
                    The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
                    $endgroup$
                    – Jo King
                    4 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
                    4 hours ago















                  1












                  $begingroup$


                  Perl 6, 30 bytes





                  +$_&&1+max map &?BLOCK,.[^2]


                  Try it online!



                  Input is a 3-element list (l, r, v).






                  share|improve this answer









                  $endgroup$














                  • $begingroup$
                    The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
                    $endgroup$
                    – Jo King
                    4 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
                    4 hours ago













                  1












                  1








                  1





                  $begingroup$


                  Perl 6, 30 bytes





                  +$_&&1+max map &?BLOCK,.[^2]


                  Try it online!



                  Input is a 3-element list (l, r, v).






                  share|improve this answer









                  $endgroup$




                  Perl 6, 30 bytes





                  +$_&&1+max map &?BLOCK,.[^2]


                  Try it online!



                  Input is a 3-element list (l, r, v).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  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
                    4 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
                    4 hours ago
















                  • $begingroup$
                    The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
                    $endgroup$
                    – Jo King
                    4 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
                    4 hours ago















                  $begingroup$
                  The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
                  $endgroup$
                  – Jo King
                  4 hours ago




                  $begingroup$
                  The &?BLOCK trick is interesting but it's a couple of bytes shorter to assign the block to $!
                  $endgroup$
                  – Jo King
                  4 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
                  4 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
                  4 hours ago











                  1












                  $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;





                  share|improve this answer










                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  $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
                    5 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
                    5 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












                  $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;





                  share|improve this answer










                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  $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
                    5 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
                    5 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








                  1





                  $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;





                  share|improve this answer










                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  $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;






                  share|improve this answer










                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share|improve this answer



                  share|improve this answer








                  edited 4 hours ago





















                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  answered 7 hours ago









                  T. SalimT. Salim

                  476 bronze badges




                  476 bronze badges




                  New contributor



                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




                  New contributor




                  T. Salim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.












                  • 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
                    5 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
                    5 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




                    $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
                    5 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
                    5 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
                  5 hours ago




                  $begingroup$
                  @ErikF Or 45 bytes
                  $endgroup$
                  – Arnauld
                  5 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
                  5 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
                  5 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











                  0












                  $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!






                  share|improve this answer











                  $endgroup$



















                    0












                    $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!






                    share|improve this answer











                    $endgroup$

















                      0












                      0








                      0





                      $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!






                      share|improve this answer











                      $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!







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 4 hours ago

























                      answered 4 hours ago









                      Zachary CottonZachary Cotton

                      4992 silver badges5 bronze badges




                      4992 silver badges5 bronze badges
























                          0












                          $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.






                          share|improve this answer









                          $endgroup$



















                            0












                            $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.






                            share|improve this answer









                            $endgroup$

















                              0












                              0








                              0





                              $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.






                              share|improve this answer









                              $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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 4 hours ago









                              NeilNeil

                              86.7k8 gold badges46 silver badges183 bronze badges




                              86.7k8 gold badges46 silver badges183 bronze badges
























                                  0












                                  $begingroup$


                                  Japt, 9 bytes



                                  Ω¡ÒßXÃrw


                                  Try it






                                  share|improve this answer









                                  $endgroup$



















                                    0












                                    $begingroup$


                                    Japt, 9 bytes



                                    Ω¡ÒßXÃrw


                                    Try it






                                    share|improve this answer









                                    $endgroup$

















                                      0












                                      0








                                      0





                                      $begingroup$


                                      Japt, 9 bytes



                                      Ω¡ÒßXÃrw


                                      Try it






                                      share|improve this answer









                                      $endgroup$




                                      Japt, 9 bytes



                                      Ω¡ÒßXÃrw


                                      Try it







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 3 hours ago









                                      ShaggyShaggy

                                      20.7k3 gold badges20 silver badges69 bronze badges




                                      20.7k3 gold badges20 silver badges69 bronze badges























                                          T. Salim is a new contributor. Be nice, and check out our Code of Conduct.









                                          draft saved

                                          draft discarded


















                                          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).




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу

                                          Israel Cuprins Etimologie | Istorie | Geografie | Politică | Demografie | Educație | Economie | Cultură | Note explicative | Note bibliografice | Bibliografie | Legături externe | Meniu de navigaresite web oficialfacebooktweeterGoogle+Instagramcanal YouTubeInstagramtextmodificaremodificarewww.technion.ac.ilnew.huji.ac.ilwww.weizmann.ac.ilwww1.biu.ac.ilenglish.tau.ac.ilwww.haifa.ac.ilin.bgu.ac.ilwww.openu.ac.ilwww.ariel.ac.ilCIA FactbookHarta Israelului"Negotiating Jerusalem," Palestine–Israel JournalThe Schizoid Nature of Modern Hebrew: A Slavic Language in Search of a Semitic Past„Arabic in Israel: an official language and a cultural bridge”„Latest Population Statistics for Israel”„Israel Population”„Tables”„Report for Selected Countries and Subjects”Human Development Report 2016: Human Development for Everyone„Distribution of family income - Gini index”The World FactbookJerusalem Law„Israel”„Israel”„Zionist Leaders: David Ben-Gurion 1886–1973”„The status of Jerusalem”„Analysis: Kadima's big plans”„Israel's Hard-Learned Lessons”„The Legacy of Undefined Borders, Tel Aviv Notes No. 40, 5 iunie 2002”„Israel Journal: A Land Without Borders”„Population”„Israel closes decade with population of 7.5 million”Time Series-DataBank„Selected Statistics on Jerusalem Day 2007 (Hebrew)”Golan belongs to Syria, Druze protestGlobal Survey 2006: Middle East Progress Amid Global Gains in FreedomWHO: Life expectancy in Israel among highest in the worldInternational Monetary Fund, World Economic Outlook Database, April 2011: Nominal GDP list of countries. Data for the year 2010.„Israel's accession to the OECD”Popular Opinion„On the Move”Hosea 12:5„Walking the Bible Timeline”„Palestine: History”„Return to Zion”An invention called 'the Jewish people' – Haaretz – Israel NewsoriginalJewish and Non-Jewish Population of Palestine-Israel (1517–2004)ImmigrationJewishvirtuallibrary.orgChapter One: The Heralders of Zionism„The birth of modern Israel: A scrap of paper that changed history”„League of Nations: The Mandate for Palestine, 24 iulie 1922”The Population of Palestine Prior to 1948originalBackground Paper No. 47 (ST/DPI/SER.A/47)History: Foreign DominationTwo Hundred and Seventh Plenary Meeting„Israel (Labor Zionism)”Population, by Religion and Population GroupThe Suez CrisisAdolf EichmannJustice Ministry Reply to Amnesty International Report„The Interregnum”Israel Ministry of Foreign Affairs – The Palestinian National Covenant- July 1968Research on terrorism: trends, achievements & failuresThe Routledge Atlas of the Arab–Israeli conflict: The Complete History of the Struggle and the Efforts to Resolve It"George Habash, Palestinian Terrorism Tactician, Dies at 82."„1973: Arab states attack Israeli forces”Agranat Commission„Has Israel Annexed East Jerusalem?”original„After 4 Years, Intifada Still Smolders”From the End of the Cold War to 2001originalThe Oslo Accords, 1993Israel-PLO Recognition – Exchange of Letters between PM Rabin and Chairman Arafat – Sept 9- 1993Foundation for Middle East PeaceSources of Population Growth: Total Israeli Population and Settler Population, 1991–2003original„Israel marks Rabin assassination”The Wye River Memorandumoriginal„West Bank barrier route disputed, Israeli missile kills 2”"Permanent Ceasefire to Be Based on Creation Of Buffer Zone Free of Armed Personnel Other than UN, Lebanese Forces"„Hezbollah kills 8 soldiers, kidnaps two in offensive on northern border”„Olmert confirms peace talks with Syria”„Battleground Gaza: Israeli ground forces invade the strip”„IDF begins Gaza troop withdrawal, hours after ending 3-week offensive”„THE LAND: Geography and Climate”„Area of districts, sub-districts, natural regions and lakes”„Israel - Geography”„Makhteshim Country”Israel and the Palestinian Territories„Makhtesh Ramon”„The Living Dead Sea”„Temperatures reach record high in Pakistan”„Climate Extremes In Israel”Israel in figures„Deuteronom”„JNF: 240 million trees planted since 1901”„Vegetation of Israel and Neighboring Countries”Environmental Law in Israel„Executive branch”„Israel's election process explained”„The Electoral System in Israel”„Constitution for Israel”„All 120 incoming Knesset members”„Statul ISRAEL”„The Judiciary: The Court System”„Israel's high court unique in region”„Israel and the International Criminal Court: A Legal Battlefield”„Localities and population, by population group, district, sub-district and natural region”„Israel: Districts, Major Cities, Urban Localities & Metropolitan Areas”„Israel-Egypt Relations: Background & Overview of Peace Treaty”„Solana to Haaretz: New Rules of War Needed for Age of Terror”„Israel's Announcement Regarding Settlements”„United Nations Security Council Resolution 497”„Security Council resolution 478 (1980) on the status of Jerusalem”„Arabs will ask U.N. to seek razing of Israeli wall”„Olmert: Willing to trade land for peace”„Mapping Peace between Syria and Israel”„Egypt: Israel must accept the land-for-peace formula”„Israel: Age structure from 2005 to 2015”„Global, regional, and national disability-adjusted life years (DALYs) for 306 diseases and injuries and healthy life expectancy (HALE) for 188 countries, 1990–2013: quantifying the epidemiological transition”10.1016/S0140-6736(15)61340-X„World Health Statistics 2014”„Life expectancy for Israeli men world's 4th highest”„Family Structure and Well-Being Across Israel's Diverse Population”„Fertility among Jewish and Muslim Women in Israel, by Level of Religiosity, 1979-2009”„Israel leaders in birth rate, but poverty major challenge”„Ethnic Groups”„Israel's population: Over 8.5 million”„Israel - Ethnic groups”„Jews, by country of origin and age”„Minority Communities in Israel: Background & Overview”„Israel”„Language in Israel”„Selected Data from the 2011 Social Survey on Mastery of the Hebrew Language and Usage of Languages”„Religions”„5 facts about Israeli Druze, a unique religious and ethnic group”„Israël”Israel Country Study Guide„Haredi city in Negev – blessing or curse?”„New town Harish harbors hopes of being more than another Pleasantville”„List of localities, in alphabetical order”„Muncitorii români, doriți în Israel”„Prietenia româno-israeliană la nevoie se cunoaște”„The Higher Education System in Israel”„Middle East”„Academic Ranking of World Universities 2016”„Israel”„Israel”„Jewish Nobel Prize Winners”„All Nobel Prizes in Literature”„All Nobel Peace Prizes”„All Prizes in Economic Sciences”„All Nobel Prizes in Chemistry”„List of Fields Medallists”„Sakharov Prize”„Țara care și-a sfidat "destinul" și se bate umăr la umăr cu Silicon Valley”„Apple's R&D center in Israel grew to about 800 employees”„Tim Cook: Apple's Herzliya R&D center second-largest in world”„Lecții de economie de la Israel”„Land use”Israel Investment and Business GuideA Country Study: IsraelCentral Bureau of StatisticsFlorin Diaconu, „Kadima: Flexibilitate și pragmatism, dar nici un compromis în chestiuni vitale", în Revista Institutului Diplomatic Român, anul I, numărul I, semestrul I, 2006, pp. 71-72Florin Diaconu, „Likud: Dreapta israeliană constant opusă retrocedării teritoriilor cureite prin luptă în 1967", în Revista Institutului Diplomatic Român, anul I, numărul I, semestrul I, 2006, pp. 73-74MassadaIsraelul a crescut in 50 de ani cât alte state intr-un mileniuIsrael Government PortalIsraelIsraelIsraelmmmmmXX451232cb118646298(data)4027808-634110000 0004 0372 0767n7900328503691455-bb46-37e3-91d2-cb064a35ffcc1003570400564274ge1294033523775214929302638955X146498911146498911

                                          Кастелфранко ди Сопра Становништво Референце Спољашње везе Мени за навигацију43°37′18″ СГШ; 11°33′32″ ИГД / 43.62156° СГШ; 11.55885° ИГД / 43.62156; 11.5588543°37′18″ СГШ; 11°33′32″ ИГД / 43.62156° СГШ; 11.55885° ИГД / 43.62156; 11.558853179688„The GeoNames geographical database”„Istituto Nazionale di Statistica”проширитиууWorldCat156923403n850174324558639-1cb14643287r(подаци)