largest binary tree

original title

https://leetcode.cn/problems/maximum-binary-tree/

train of thought

insert image description here
When we get this question, it is a binary tree question type, so we will soon realize whether we need to use depth-first and breadth-first to answer. Depth-first, breadth-first templates . Seeing that recursion is mentioned in the title, isn't this a direct depth-first search? Then look at the topic and find that for any node, first find the maximum value in the interval, and then build the node, which makes it more certain to use the depth-first algorithm (the depth-first algorithm generally uses recursion).

Depth-first algorithm Some questions are easy to have no ideas. Don't panic, get a recursive question, first think about what is a step in this question ?

Obviously, this question is: pass in an array and two boundaries, then find the maximum value in the array according to the boundaries, and then build a node. And all the nodes down are the above operation (that is, repeated, so the bold one in front is a step )

Then determine the parameters and return value.
Incoming parameters: We know earlier that an array and two boundaries need to be passed in.
Return value: According to the requirements of the topic, we know that we need to return after building this node, so the return value is the created node.
Boundary conditions: Consider the case where the input array is empty at the beginning, and there is no number at this time, that is, no node needs to be created, and null is returned directly; during the consideration process, the boundary is reduced to none, and in this case, no node needs to be created , also returns null directly. So the final boundary condition is to return null directly when the boundary is reduced to nothing.
Process: According to the recursive template, when we pass in parameters, we only need to consider one step . First, write the boundary conditions first. Next, we handle this step , that is, first find the largest value in the array and the boundary at this time, and then create a node with the value of this value, and then do the same operation for its left subtree and right subtree? Yes, then call recursion at this time, the returned node is the left and right of the current node, and finally return to the root node.

That is to say, when we get a question about a binary tree, we must first determine which of the three , the root node , the left subtree , and the right subtree, should be processed first, and then correspond to each other (pre-order traversal, middle-order traversal, post-order traversal) ). This question is obviously to create the root node before processing the left and right subtrees, which corresponds to the preorder traversal. Then our idea of ​​processing the root node is also very clear, which is to find the largest node, then build the node, and then process the left subtree and right subtree (obviously recursive processing), the returned node is just the left and right of the construction node, and finally return to the construction node.

the code

class Solution {
    
    
    public TreeNode constructMaximumBinaryTree(int[] nums) {
    
    
        return maximumBinaryTree(nums,0,nums.length -1);
    }
    public TreeNode maximumBinaryTree(int[] nums,int start,int end){
    
    
        if(start > end){
    
    
            return null;
        }
        int maxNum = Integer.MIN_VALUE;
        int maxIndex = 0;
        for(int i = start;i <= end;i++){
    
    
            if(nums[i] > maxNum){
    
    
                maxNum = nums[i];
                maxIndex = i;
            }
        }
        TreeNode root = new TreeNode(maxNum);
        root.left = maximumBinaryTree(nums,start,maxIndex - 1);
        root.right = maximumBinaryTree(nums,maxIndex + 1,end);
        return root;
    }
}

Guess you like

Origin blog.csdn.net/PaperJack/article/details/125323045