Java代码实现求二叉树深度

这道题是2014笔试农商银行的一道题目

给出一个类,写一个方法求二叉树深度

class Tree {
/*
 *二叉树类
*/
    int data;
    Tree left;
    Tree right;
}

用递归思想实现

public class Demo{

    public static int GetDept(Tree t) {

        int l, r;
        if (t.equals(null)) {
            return 0;
        } else {
            l = GetDept(t.left);
            r = GetDept(t.right);
        }

        if (l > r)
            return l + 1;
        else
            return r + 1;
    }
}

 

posted @ 2014-10-19 18:54  mjhuang  阅读(445)  评论(0编辑  收藏  举报