二叉树的高度是从根节点到离根最远的节点子节点的距离。
以下是一个例子:
2 <-- root: Height 1
/ \
7 5 <-- Height 2
/ \ \
2 6 9 <-- Height 3
/ \ /
5 11 4 <-- Height 4 二叉树高度:4
二叉树的Definition
树是包含有符号整数值以及其他两个树或指向它们的指针的对象。
二叉树结构的结构如下所示:
typedef struct tree
{
struct tree * l;
struct tree * r;
int v;
} tree;挑战:
输入
二叉树的根
输出
表示二叉树高度的数字。
假设给定二叉树的根作为输入,编写计算二叉树高度并返回高度的最短程序。字节数最少的程序(记帐白空间)获胜。
发布于 2019-08-04 17:52:43
发布于 2019-08-04 20:19:08
h L=0
h(N l r _)=1+max(h l)(h r)使用自定义树类型data T = L | N T T Int,这是Haskell等价于在挑战中给出的C结构。
发布于 2019-08-04 21:20:13
{($_,{.[*;*]}...*eqv*)-2}输入是一个3元素列表(l, r, v).空树是空列表。
{ } # 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{+$_&&1+max map &?BLOCK,.[^2]}https://codegolf.stackexchange.com/questions/189220
复制相似问题