33,028
社区成员
发帖
与我相关
我的任务
分享/*
二叉树的深度定义为二叉树中层数最大的叶结点的层数。
二叉树的高度定义为二叉树中层数最大的叶结点的层数加1。
*/
using System;
class Tree
{
public Tree LChild = null; // 左子树
public Tree RChild = null; // 右子树
// 树的深度
public static int Depth(Tree t)
{
if (t == null) return -1;
return Math.Max(Tree.Depth(t.LChild), Tree.Depth(t.RChild)) + 1;
}
// 树的高度
public static int Height(Tree t)
{
return Tree.Depth(t) + 1;
}
}
class Program
{
static void Main()
{
Tree child = new Tree();
child.LChild = new Tree();
Console.WriteLine(Tree.Depth(child));
Tree tree = new Tree();
tree.RChild = child;
Console.WriteLine(Tree.Depth(tree));
}
}using System;
class Tree
{
public Tree LChild = null; // 左子树
public Tree RChild = null; // 右子树
// 树的深度
public static int Depth(Tree t)
{
if (t == null) return 0;
return Math.Max(Tree.Depth(t.LChild), Tree.Depth(t.RChild)) + 1;
}
}
class Program
{
static void Main()
{
Tree child = new Tree();
child.LChild = new Tree();
Console.WriteLine(Tree.Depth(child));
Tree tree = new Tree();
tree.RChild = child;
Console.WriteLine(Tree.Depth(tree));
}
}int depth(Tree T)
{
if(T == NULL) return 0;
return
depth(T->lchild)>depth(T-rchild) ? 1+depth(T->lchild) : 1+depth(T->rchild);
}