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.
h
. Might be better to define a specific structure made just of lists for the purpose of this challenge. \$\endgroup\$[root_value, left_node, right_node]
where each ofleft_node
andright_node
are also binary trees acceptable? It'll be trivial in many languages, but might be fun in some others. \$\endgroup\$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\$