[Question]
  [
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.
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
≈í·∏ä‚Äô
```
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!](https://tio.run/##y0rNyan8///opIc7uh41zPz//3@0kU60uQ6IjAUiIDbTiTaFcwwNoUwQByJsqRNtApcHSQAA "Jelly ‚Äì Try It Online")**
### How?
Pretty trivial in Jelly:
```
≈í·∏ä‚Äô - Link: list, as described above
≈í·∏ä  - depth
  ‚Äô - decremented (since leaves are `[value, [], []]`)
```
[Answer]
# [Python 2](https://docs.python.org/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!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIdEq0S46NjEvRbcuN7FCIzexQCNNJ1FT83@JbWZeQWmJhiZXQVFmXolCmkaJ5v9oI51ocx0QGQtEQGymE20K5xgaQpkgDkTYUifaBC4PkgAA "Python 2 ‚Äì Try It Online")**
Note that `[]` will return `False`, but in Python `False==0`.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~11~~ ~~7~~ 5 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Œî‚Ç¨`}N
```
-4 bytes thanks to *@ExpiredData*.  
-2 bytes thanks to *@Grimy*.
Input format is similar as the Jelly answer: a list representing the tree: `[root_value, left_tree, right_tree]`, where each of `left_tree` and `right_tree` are similar structures (optionally empty). I.e. `[2,[7,[2,[],[]],[6,[5,[],[]],[11,[],[]]]],[5,[],[9,[4,[],[]],[]]]]` represents the tree from the challenge description.
[Try it online](https://tio.run/##yy9OTMpM/f//3JRHTWsSav3@/4820ok21wGRsUAExGY60aZwjqEhlAniQIQtdaJN4PIgCQA) or [verify a few more test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeX/c1MeNa1JqPX7r6N3aOv/6GgjnWhzHRAZC0RAbKYTbQrnGBpCmSAORNhSJ9oELg@WUFAAchWiYUqBTBwqAQ).
**Explanation:**
```
Œî     # Loop until the (implicit) input-list no longer changes:
  ‚Ç¨`  #  Flatten the list one level
}N    # After the loop: push the 0-based index of the loop we just finished
      # (which is output implicitly as result)
```
Note that although 05AB1E is 0-based, the changes-loop `Œî` causes the output index to be correct, because it needs an additional iteration to check it no longer changes.
[Answer]
## 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!](https://tio.run/##y0gszk7NyfmfkliSqBCiYKvgo1Cj4AdkhSh45pX8z1DwsTVQ4MrQ8FPIUShSiNe0NdTOTazQyFDI0QQSRZr/cxMz84DaCooy80oUVBQyFIBKIcgHCI00kTimmjCWoaGmgpmmgjlUAKHERBNIWGqClBpp/gcA "Haskell ‚Äì Try It Online")
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 25 bytes
```
{($_,{.[*;*]}...*eqv*)-2}
```
Input is a 3-element list `(l, r, v)`. The empty tree is the empty list.
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WkMlXqdaL1rLWiu2Vk9PTyu1sExLU9eo9n9xYqVCGlBWUyEtv4hLQ1OHSwNIKICwIZiDxEMSBXGQZbCoRZP@DwA "Perl 6 ‚Äì Try It Online")
### Explanation
```
{                       }  # Anonymous block
    ,        ...  # Sequence constructor
  $_  # Start with input
     {.[*;*]}  # Compute next element by flattening one level
               # Sadly *[*;*] doesn't work for some reason
                *eqv*  # Until elements doesn't change
 (                   )-2  # Size of sequence minus 2
```
### Old solution, 30 bytes
```
{+$_&&1+max map &?BLOCK,.[^2]}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WlslXk3NUDs3sUIhN7FAQc3eycff2VtHLzrOKLb2f3FipUKahkq8pkJafhGXX2aODpcGiFQAE4aaQC4qH0kCzEKTxaYDU8l/AA "Perl 6 ‚Äì Try It Online")
[Answer]
# 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!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zVYjOlEnKVbT1i7R3lA7TSNNI1HTLk0jSdM@0SpJ08rgf3J@XnF@TqpeTn46UDIaCGJ1FEDYCETDeaZgHpRjaAgkzIDYHC4KlzOB0pZQTUaxmpr/AQ "JavaScript (Node.js) ‚Äì 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
```
[Answer]
# 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;
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 32 bytes
```
f=a=>/,,/.test(a)&&f(a.flat())+1
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zTbR1k5fR0dfryS1uEQjUVNNLU0jUS8tJ7FEQ1NT2/B/cn5ecX5Oql5OfrpGmkY0EMTqKICwEYiG80zBPCjH0BBImAGxOVwULmcCpS2hmoxiNTX/AwA "JavaScript (Node.js) ‚Äì Try It Online")
~~Using the name `flat` instead of `flatten` or `smoosh` is a great idea for code golf.~~
Using `[]` for null node in the tree, and `[left, right, value]` for nodes. `value` here is an integer.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 10 bytes
```
Depth@#-2&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2ar8d8ltaAkw0FZ10jtv6Y1V0BRZl5JdFp0tZGOQrU5EIPoWhAGEWZAbIosYGgI54H5IElLIDZBVgQlamNj/wMA "Wolfram Language (Mathematica) ‚Äì Try It Online") Takes input as a nested list `{v, l, r}`.
[Answer]
# Haskell, 28 bytes
Using the following data definition:
```
data T a = (:&) a [T a]
```
The height is:
```
h(_:&x)=foldr(max.succ.h)0 x
```
[Answer]
# 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!](https://tio.run/##K07OSM1N1QWSVf//a6SkpmXmpWqkKWRoamSmaeSV5uTYA9kGGtoKhhq5iRUaaRrJiUUaySlFQFFNTQQXLgQGQIMyiwtyEisVgEapawBFuOAiSjF5SkhcsAJDBQ1NBZAyotQZwVUTVg5TC2YZwznGCCOIMgOo3gSuWZOQif8B) 
[Answer]
# [R](https://www.r-project.org/), 51 bytes
```
function(L){while(is.list(L<-unlist(L,F)))T=T+1;+T}
```
[Try it online!](https://tio.run/##lU89D4IwEN37Ky5xaQNoWgoGP1bj0Djh5mZEmxBIoMbB@NtrKRoOdbHJvbvevfd6bWyxtsW1OhpdV1Sx@@2iyxPV7bTUraFqFV2rvgo3jLF8nQd8GeQPO4HhCFhFETR1bRawPenzxQAniDCDA7rOXSRe8eKKYeiYDge2cJG6yDA//vB2gDoJAOcgsUACIapbEvxXROjTPNztlfLA@k6COoyo@Fvxun0KUywcefk6w2NnLP8w/lqs73A@8vz1pk8SK98LkIIqwTqMPcoOPT3GrvYJ "R ‚Äì Try It Online")
* **Input:** a nested list in the format : `list(ROOT_ELEMENT, LEFT_TREE, RIGHT_TREE)`
* **Algorithm:** Iteratively flattens the tree by one level until it becomes a flat vector : the iterations count corresponds to the max depth.
Inspired by [@KevinCruijssen solution](https://codegolf.stackexchange.com/a/189241/41725)
---
Recursive alternative :
# [R](https://www.r-project.org/), 64 bytes
```
`~`=function(L,d=0)'if'(is.list(L),max(L[[2]]~d+1,L[[3]]~d+1),d)
```
[Try it online!](https://tio.run/##lU9NC8IwDL33VwQ8uGKn9mPKRO8eijdPKijOj4I60Ame9tdn7JRl04uFl6TJe6/ptSjW@Xqyv1@2mUsvgRXJpM/bbt8O3K17crcssFycN4/ALhZqtcqTjhRY6rLkIuFFC6qjYByGcE3TbATTnTscM5CMEHqwJNchIvKKN1dVQ2RirNgKMUDElK8b3hhIJwKQEgwVGGDMvpYE/zclfBqK2dxaH3jZiUiHM6u/Fe9bUzigwpqXr2M6RmPzh/HXYmVHyprnrzd9MlT5WYDlViE0wrDc8zThFU8 "R ‚Äì Try It Online")
Redefines the function/operator `'~'` making it able to compute the max depth of a tree stored in a list structure.
The list structure of a tree is in the format : `list(ROOT_ELEMENT, LEFT_TREE, RIGHT_TREE)`
* -2 thanks to @Giuseppe
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 bytes
```
@eU=c1}a
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=QGVVPWMxfWE&input=WzIsWzcsWzIsW10sW11dLFs2LFs1LFtdLFtdXSxbMTEsW10sW11dXV0sWzUsW10sWzksWzQsW10sW11dLFtdXV1d)
## Original, 9 bytes
```
√é¬©¬°√í√üX√Érw
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=zqmh0t9Yw3J3&input=WzIsWzcsWzIsW10sW11dLFs2LFs1LFtdLFtdXSxbMTEsW10sW11dXV0sWzUsW10sWzksWzQsW10sW11dLFtdXV1d)
[Answer]
# Kotlin, 45 bytes
```
val Tree.h:Int get()=1+maxOf(l?.h?:0,r?.h?:0)
```
Assuming the following class is defined
```
class Tree(var v: Int, var l: Tree? = null, var r: Tree? = null)
```
[Try it online](https://pl.kotl.in/K4PmzcvGl)
[Answer]
# [K (ngn/k)](https://gitlab.com/n9n/k), 4 bytes
**Solution:**
```
#,/\
```
[Try it online!](https://tio.run/##y9bNS8/7/19ZRz9Gw8haw9xaQ8dI01rDDEibAmkdQ0NNTSBtaq0BJC2BAiZAGiik@f8/AA "K (ngn/k) ‚Äì Try It Online")
**Explanation:**
I think I may have missed the point.
Representing a tree as the 3-item list (parent-node;left-child;right-child), the example can be represented as
```
(2;
  (7;
    (,2);
    (6;
      (,5);
      (,11)
    )
  );
  (5;
    ();
    (9;
      (,4);
      ()
    )
  )
)
```
or: `(2;(7;(,2);(6;(,5);(,11)));(5;();(9;(,4);())))`.
So the solution is to iteratively flatten, and count the iterations:
```
#,/\ / the solution
   \ / iterate
 ,/  / flatten
#    / count
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 29 bytes
```
‚äûŒ∏‚Å∞‚äûœÖŒ∏Ôº¶œÖ¬´‚âî‚äï‚äüŒπŒ∏Ôº¶Œ¶Œπ‚àßŒ∫Œª‚äûœÖ‚äûÔºØŒ∫Œ∏¬ªÔº©Œ∏
```
[Try it online!](https://tio.run/##VYxPC8IwDMXP66fIMYUKTvyDeBqC4Mndyw5lq65Yu63rvIifva7dBIWQ5L38Xspa2LIR2vt86GvsGCzpgcR9YNCN@7WxgAOFF0myvlc3g2dTWvmQxskK86ZFRemEJpE9Ke2kRcUgMxXeGWhKKXxfhnlppRWuseHY0TH4JrlVxuFR9A6D4z0nwFeMAPBd6EHwYqxiUts4gG9@XeBpOuvJGc@RnkP7mVr/hwJNCr946g8 "Charcoal ‚Äì 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.
```
Ôº¶œÖ¬´
```
Perform a breadth-first search of the tree.
```
‚âî‚äï‚äüŒπŒ∏
```
Get the depth of this node.
```
Ôº¶Œ¶Œπ‚àßŒ∫Œª
```
Loop over any child nodes.
```
‚äûœÖ‚äûÔºØŒ∫Œ∏
```
Tell the child node its parent's depth and push it to the list of all nodes.
```
¬ªÔº©Œ∏
```
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.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 5 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
‚ñê‚ñå¬µ‚ï°‚åÇ
```
[Run and debug it](https://staxlang.xyz/#p=dedde6b57f&i=[2,[7,[2,[],[]],[6,[5,[],[]],[11,[],[]]]],[5,[],[9,[4,[],[]],[]]]]&a=1)
Stax has neither pointers nor null values, so I represent the input like `[2,[7,[2,[],[]],[6,[5,[],[]],[11,[],[]]]],[5,[],[9,[4,[],[]],[]]]]`. Maybe that's an unfair advantage, but it was the closest I could get.
Unpacked, ungolfed, and commented, the code looks like this.
```
        The input starts on top of the input stack
Z       Tuck a zero underneath the top value in the stack.  Both values end up on the main stack.
D       Drop the first element from array
F       For each remaining element (the leaves) run the rest of the program
  G^    Recursively call the entire program, then increment
  T     Get maximum of the two numbers now ow the stack
```
[Run this one](https://staxlang.xyz/#c=++++%09The+input+starts+on+top+of+the+input+stack%0AZ+++%09Tuck+a+zero+underneath+the+top+value+in+the+stack.++Both+values+end+up+on+the+main+stack.%0AD+++%09Drop+the+first+element+from+array%0AF+++%09For+each+remaining+element+%28the+leaves%29+run+the+rest+of+the+program%0A++G%5E%09Recursively+call+the+entire+program,+then+increment%0A++T+%09Get+maximum+of+the+two+numbers+now+ow+the+stack&i=[2,[7,[2,[],[]],[6,[5,[],[]],[11,[],[]]]],[5,[],[9,[4,[],[]],[]]]])
[Answer]
# Julia, 27 bytes
```
f(t)=t‚â¢()&&maximum(f,t.c)+1
```
With the following struct representing the binary tree:
```
struct Tree
    c::NTuple{2,Union{Tree,Tuple{}}}
    v::Int
end
```
`c` is a tuple representing the left and right nodes and the empty tuple `()` is used to signal the absence of a node.
[Answer]
# [Kotlin](https://kotlinlang.org), 42 bytes
```
fun N.c():Int=maxOf(l?.c()?:0,r?.c()?:0)+1
```
[Try it online!](https://tio.run/##ZYwxDsIwDEXn9hQeY1GhskaUipElDJzAKmpV4RqapBUIcfaQFMGCvfz/ZL/L1XMv4UyeoGFyDoyaiYE1mBoqkIm5gETsH5k1HMRHVGJoJwGzbhTqiKqB7sdWcZ1ArcvCfhOuNsvpQL0osp3TsLeWHtuTt710O3zmWVKP0WrUZxGLX05tmTy7xQfPosakxvwV3g "Kotlin ‚Äì Try It Online")
Where
```
data class N(val l: N? = null, val r: N? = null, val v: Int = 0)
```
[Answer]
# [Jq](https://stedolan.github.io/jq/), 18 bytes
Takes input as `[Value, Left, Right]` *or* any permutation of this.
```
[paths|length]|max
 paths             # for each path as a list of indexes to follow
                   # to get to an element or array inside the input
                   # e.g. [0,2,1] to represent array[0][2][1]
      |length      # its length
[            ]     # collect into an array
              |max # the maximum of this array
```
[jq play it!](https://jqplay.org/s/nXIfxv-o6C)
]