找到二叉树的深度

时间:2014-04-24 23:03:16

标签: java binary-tree

您好我正在尝试找到二叉树的深度。我尝试的方法与传统方法不同。我的逻辑是尝试在进行深度遍历时找到最大级别。以下是代码。这种方法不起作用,因为一旦到达else部分,最大值总是为零。我不明白为什么。

public class BinaryTree {

    public static void main(String args[])
    {
        Node root = new Node(1);
        Node two = new Node(2);
        Node three = new Node(3);
        Node four = new Node(4);
        Node five = new Node(5);
        Node six = new Node(6);
        Node seven = new Node(7);
        Node eight = new Node(8);

        root.left = two;
        root.right = three;
        two.left = four;
        two.right = five;
        three.left = six;
        three.right = seven;
        five.left = eight;
        findDepth(root,0,0);

    }


    public static void findDepth(Node root,int level, int max)
    {

        if(root==null)
            return ;
        else{

            if(root.left==null && root.right==null)
            {
                if(max<level)
                {

                    max=level;

                }
                level=level-1;
            }

            findDepth(root.left,level+1,max);
            findDepth(root.right,level+1,max);

        }
    }

}

// The Node class

public class Node {

    int data;
    Node left;
    Node right;

    public Node(int i) {
        this.data = i;
    }

}

我这样做了,效果很好

public static int findDepth(Node root,int level, int max1, String str)
{

    if(root==null)
        return 0;

    if(max<level)
    {

        max=level;

    }
        findDepth(root.left,level+1,max,"left");
        findDepth(root.right,level+1,max,"right");

        return max;

}

这里max是类的静态成员。但我想指出堆栈溢出中的另一篇文章,他们说这个值将传播到下一个递归调用。

Print all paths from root to leaf in a Binary tree

此处列表的旧值将传播到所有递归调用

2 个答案:

答案 0 :(得分:0)

作为参数变量,max的行为类似于局部变量。意味着每个递归都有自己的变量max副本。而且由于你没有返回任何东西,你将抛弃该值,所以在整个递归结束时,第一次调用函数中max的值仍为零。

考虑在函数中使max为静态变量,而不是将其作为参数传递。

顺便说一下,你的方法仍有问题,你的函数findDepth没有“找到”任何东西,因为它没有返回任何东西。您也可以在每个级别返回最大值但始终忽略它,因此只有findDepth的调用者实际处理最大值。但是这种方法正在轻轻地推动你回到原来的方式去做:)。

更新

Asker有一个跟进问题。

  

但是我想指出堆栈溢出中的另一篇文章,他们说这个值会传播到下一个递归调用。

正如user430788所解释的那样,max是原始类型int,按值传递,而ArraList<Integer>则不是,因此它将通过引用传递,并将由该函数调用的所有实例共享

答案 1 :(得分:0)

Max是一种原始类型。所有Java基元类型都按值传递,因此在函数内更改它们不会影响它们在调用者中的值。

您需要返回新的最大值。此外,您的递归代码有点不稳定。

试试这个......

public class BinaryTree {

    public static void main(String args[])
    {
        Node root = new Node(1);
        Node two = new Node(2);
        Node three = new Node(3);
        Node four = new Node(4);
        Node five = new Node(5);
        Node six = new Node(6);
        Node seven = new Node(7);
        Node eight = new Node(8);

        root.left = two;
        root.right = three;
        two.left = four;
        two.right = five;
        three.left = six;
        three.right = seven;
        five.left = eight;
        findDepth(root,0,0);

    }


    public static int findDepth(Node root,int level)
    {

        if(root==null){
            return level;
        }else{
            return Math.Max(findDepth(root.left,level+1,max),
                            findDepth(root.right,level+1,max));
        }
    }

}

// The Node class

public class Node {

    int data;
    Node left;
    Node right;

    public Node(int i) {
        this.data = i;
    }

}

当你打电话给它时,它应该被称为findDepth(treeRoot,0);

递归逻辑说的是“当我们走到树下时,每层递增1级。当我们到达底部时,返回级别。然后对于底部上方的每个节点,取两者返回的级别我们下面的节点返回更深层次。

相关问题