数据结构2-二叉树的最大深度

来源:互联网 发布:禅道 linux 启动 编辑:程序博客网 时间:2024/06/16 03:15

获取到二叉树的最大深度主要是使用递归的方式来完成,思路分为下面三步:

(1)判断传来的结点是否为空,这是递归的出口;

(2)递归调用来累加该结点的左右子树的深度;

(3)返回左右子树深度更大的值。

private static int MaxDepth(BiTree root){    if (root == null) return 0;    int left = 1;    int right = 1;    left += MaxDepth(LeftChild(root));    right += MaxDepth(RightChild(root));    return left > right ? left : right;}

原创粉丝点击