在Java中二叉树的遍历中获取树的最小和最大高度?

时间:2013-11-06 02:09:30

标签: java tree height binary-tree tree-traversal

我有两种方法可以在Java中获取二叉树的最小和最大高度。但我两次通过root进行两次遍历。每个都是log n in Big(O)。有没有办法在相同的遍历中计算最小值和最大值,并返回一个数组,其中两个索引对应于最小值和最大值。

以下是我的方法

public static int minHeight(Node root){
            if (root==null) return -1;
            return 1+Math.min(minHeight(root.left), minHeight(root.right));
        }

public static int height(Node root){
            if (root==null) return -1;
            return 1+Math.max(height(root.left), height(root.right));

        }

class Node {
        Node left;
        Node right;
        int data;

        public Node(int c){
            this(c, null, null);
        }
        public Node(int c,Node left, Node right) {
            this.data = c;
            this.left=left;
            this.right=right;
        }


    }

4 个答案:

答案 0 :(得分:2)

只需计算一次通过的高度,并随时跟踪最小值和最大值。您的困惑在于您的函数返回单个值,但实际上您需要确定一对值。因此,您可以传入一个对象,该对象具有可存储结果的字段,并通过该字段返回结果,而不是通过函数的返回值:

class MinMax {
    public int min;
    public int max;
}

void computeMinMaxHeight (Node root, MinMax minmax) {
    // update the fields of minmax accordingly
}

初始化MinMax字段的便捷方法可能是:

class MinMax {
    public int min = Integer.MAX_VALUE;
    public int max = Integer.MIN_VALUE;
}

或添加一些表示未初始化的标志,因此第一项的值会正确填写。

编辑:你也可以像Changgeng所说的那样返回一个int[2]数组;它只取决于你发现在语义上更合适的东西。就个人而言,我选择MinMax之类的东西(Java实际上并没有用于表示值范围的标准类),而且将输出参数传递给函数可以节省对象分配(如果有意义的话)。 / p>

答案 1 :(得分:1)

您对Big(O)的断言不正确。在您的实现中,您需要访问树中的每个节点,因此时间复杂度为O(n)

级别顺序树遍历可以一次给出答案,但是你需要两个队列来正确地完成这个。

public int[] findMinMax(Node node) {
    Queue<Node> currentLevel = new LinkedList<Node>();
    Queue<Node> nextLevel = new LinkedList<Node>();

    currentLevel.offer(node);
    int currentHeight = 1;

    int[] result = new int[]{Integer.MAX_VALUE, Integer.MIN_VALUE};
    while (!currentLevel.isEmpty() || !nextLevel.isEmpty()) {

        if (currentLevel.isEmpty()) {
            currentHeight += 1;
            Queue<Node> tmp = nextLevel;
            nextLevel = currentLevel;
            currentLevel = tmp;
        }

        node = currentLevel.poll();
        if (node.left != null) {
            nextLevel.offer(node.left);
        }
        if (node.right != null) {
            nextLevel.offer(node.right);
        }
        if (node.left == null && node.right == null) {
            result[0] = Math.min(result[0], currentHeight);
        }
    }

    result[1] = currentHeight;

    return result;


}

说,这通常不值得。递归解决方案更容易编写和理解。

答案 2 :(得分:0)

每个节点上总是可以有两个属性,显示节点的最小和最大高度。

  this.max = Math.max(this.left.max,this.right.max) + 1 ; 
  this.min = Math.min(this.left.min,this.right.min) + 1;

答案 3 :(得分:0)

公共类MinMax {

public void printMinMaxNumbers(int[] nums){
    int min = nums[0];
    int max = nums[1];
    for(int n:nums){
        if(n < min){
            min = n;
        } else if(n > max){
            max = n;
        }
    }
    System.out.println("Minimum Number: "+min);
    System.out.println("Maximum Number: "+max);
}

public static void main(String a[]){
    int num[] = {5,34,78,21,79,12,97,23};
    MinMax tmn = new MinMax();
    tmn.printMinMaxNumbers(num);
}

}