[Programmer Code Interview Guide] binary tree problem - find the largest binary search binary water (a tree dp)

The meaning of problems

Given to the head node of a binary tree, known values ​​are not the same for all the nodes, the nodes containing up to find two forks search tree, and return to the head node of the tree.

answer

  • Implemented in postorder process.
  • Solution step by step listed dp tree. The possibility of three types: left subtree largest, the largest right subtree, the current node is the root of the tree.
  • Time complexity of O (n)

Tree dp

Use the premise

If the goal is to solve the subject s rule, then the process can be solved as in each node is a root node of the subtree at each answer s rule, and in which a certain final answer.

step

  • possibility. Root, left subtree, right subtree point of view.
  • It lists the variables required to get all of the layers present. Left and right subtrees to be unified.
  • In the recursive function to write: basecase, about recursion, determine the possibility of updating to give all the variables on the floor.

all

It has been unsolved by the subclasses of the class how to do. Subclass must be an example and Main was not any instance when OJ measured?

Code

public class ReturnType{
    int min;//最小节点值
    int max;//最大节点值
    int maxSize;//最大BST树的大小
    Node maxRoot;//最大BST树的根节点
    
    ReturnType(int min,int max,int maxSize,Node maxRoot){
        this.min=min;
        this.max=max;
        this.maxSize=maxSize;
        this.maxRoot=maxRoot;
    }
}
public class Main {
    public static void main(String args[]) {
        //test
        Node root=new Node(10);
        Node node1=new Node(11);
        Node node2=new Node(14);
        Node node3=new Node(11);
        Node node4=new Node(15);
        root.left=node1;
        root.right=node2;
        node2.left=node3;
        node2.right=node4;
        
        int maxSize=getMaxBST(root).maxSize;
        System.out.println(maxSize);
    }
    
    public static ReturnType getMaxBST(Node root) {
        if(root==null) {
            return new ReturnType(Integer.MAX_VALUE,Integer.MIN_VALUE,0,null);
        }
        ReturnType lReturn=getMaxBST(root.left);
        ReturnType rReturn=getMaxBST(root.right);
        
        int min=getMin(root.val,lReturn.min,rReturn.min);
        int max=getMax(root.val,lReturn.max,rReturn.max);
        int maxSize=Math.max(lReturn.maxSize, rReturn.maxSize);
        Node maxRoot=maxSize==lReturn.maxSize?lReturn.maxRoot:rReturn.maxRoot;
        if(lReturn.maxRoot==root.left&&rReturn.maxRoot==root.right&&lReturn.max<root.val&&rReturn.min>root.val) {//
            maxSize=lReturn.maxSize+rReturn.maxSize+1;
            maxRoot=root;
        }
        return new ReturnType(min,max,maxSize,maxRoot);
    }
    
    private static int getMin(int... vals) {
        int min=Integer.MAX_VALUE;
        for(int val:vals) {
            if(val<min) {
                min=val;
            }
        }
        return min;
    }
    
    private static int getMax(int... vals) {
        int max=Integer.MIN_VALUE;
        for(int val:vals) {
            if(val>max) {
                max=val;
            }
        }
        return max;
    }   
}

Guess you like

Origin www.cnblogs.com/coding-gaga/p/10978987.html