如何在二叉树中查找最大BST的大小

时间:2016-03-24 18:51:03

标签: java recursion binary-search-tree

import java.util.*;

class Node
{
    int data;
    Node left , right;
    public Node (int item)
    {
        data = item;
        left = right = null;
    }
}

class BinaryTree
{
    public static Node root;
    BinaryTree()
    {
        root = null;
    }

    public int largestBST(Node root)
    {
                MinMax m = largest(root);
            return m.size;
    }

    public MinMax largest(Node root)
    {
        if(root == null)
        {
            return new MinMax();
        }

        MinMax leftMinMax = largest(root.left);
        MinMax rightMinMax = largest(root.right);

        MinMax m = new MinMax();

        if(leftMinMax.isBST == false || rightMinMax.isBST == false || (leftMinMax.max > root.data) || (rightMinMax.min <= root.data))
        {
            m.isBST = false;
            m.size = Math.max(leftMinMax.size , rightMinMax.size);
        }

        m.isBST = true;
        m.size = leftMinMax.size + rightMinMax.size + 1;

        m.min = root.left != null ? leftMinMax.min : root.data;//if left node is null take node as min or min of left

        m.max = root.right != null ? rightMinMax.max : root.data;//if right node is null take node as max or max of right

        return m;
    }

    class MinMax
    {
        int max,min;
        boolean isBST;
        int size;
        MinMax()
        {
            max = Integer.MIN_VALUE;
            min = Integer.MAX_VALUE;
            isBST = false;
            size = 0;
        }
    }

    public static void main(String args[])
    {
        BinaryTree bt = new BinaryTree();
        bt.root = new Node(25);
        bt.root.left = new Node(18);
        bt.root.right = new Node(50);
        bt.root.left.left = new Node(19);
        bt.root.left.right = new Node(20);
        bt.root.right.left = new Node(35);
        bt.root.right.right = new Node(60);
        bt.root.right.left.right = new Node(40);
        bt.root.right.left.left = new Node(20);
        bt.root.right.right.left = new Node(55);
        bt.root.right.right.right = new Node(70);

        int size = bt.largestBST(root);

        System.out.println("The size of largest BST is " + size);
    }
}

这里的输出应该是7,而是显示树中节点的总数,即11。

代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

如果您希望找到树的最大深度,那么重写largestBST(Node root)就像这样:

public int largestBST(Node root)
{
    if (root == null) {
        return 0;
    }

    return 1 + Math.max(largestBST(root.left), largestBST(root.right));
}

这里发生的是我们返回1 + max(left subtree depth, right subtree depth)。如果节点不存在,我们返回0以停止递归。这将返回4

如果最大,则表示从给定根开始的节点数最多的子树,无论深度如何,我们都可以修改此解决方案以获得以下内容:

public int largestBST(Node root)
{
    if (root == null) {
        return 0;
    }

    return Math.max(countNodes(root.left), countNodes(root.right));
}

public int countNodes(Node root)
{
    if (root == null) {
        return 0;
    }

    return 1 + countNodes(root.left) + countNodes(root.right);
}

正如您所看到的,递归现在发生在countNodes()方法中,而largestBST()只是从左右分支返回最大值。递归遵循与以前相同的原则。如果节点不存在则返回0,并且我们为每个存在的节点添加1。结果现在是7