Title 654, the maximum binary tree

I, entitled

Given an array of integers contain duplicate elements. In this array defines a maximum binary tree constructed as follows:
       1, binary tree root is the largest element in the array.
       2, the left subtree is the largest maximum value of a binary tree constructed by the left portion of the array.
       3, the right subtree is constructed by the maximum value of the right part of the array the maximum binary tree.
Construction of the binary tree by the maximum given array, and outputs the root of this tree. 1

Example:
Here Insert Picture Description

Second, the idea

The idea is to start a direct recursion
       1, first find the maximum of the number of the pile, and then create a node and a respective parent node and is connected to
       2, the next maximum value of the left and right are cut Back left subtree, right subtree
is then repeated to give 1,2 results

Finally, I'm sorry, I was too dishes, no recursion it stands to reason that it should be possible, if that month of that year I might try again spur of the recursive approach. For now it should be feasible, but not confirmed.

The actual practice: non-recursive useStackTo solve the problem
       1, the right to create an array of available directly from left to right subtree, provided that the child than the parent node has a large
       2, the value of the child node will be created than the parent node occurs when a large case,StackThe action came out and began backtracking, has been up until you find the appropriate node
       3, find the right node after node originally put the change into a left subtree right subtree, will be the new node as the right subtree
       whichStackRole: to store the parent node of the current node have been popped, then that can be the root node, each node will be created after the push, when carried out popped back

Precautions:
       1, when the cycle of the position pointer
       2, when a switching node careful not to get lost, because I was lost when switching node, three lines of code changed over 1 hour

Third, the code

import java.util.ArrayList;


public class T0654 {

    public static void main(String[] args) {
        //int nums[] = {3, 2, 1, 6, 0, 5};
        int nums[] = {2,7,0,1,11,2,4,1,5,3,12,0,6,8,3,2};
        ZHONGGEN( constructMaximumBinaryTree(nums) );
        System.out.println();
        XIANGEN( constructMaximumBinaryTree(nums) );
    }

    public static TreeNode constructMaximumBinaryTree(int[] nums) {

        TreeNode root = new TreeNode( nums[0] );
        TreeNode parent = root;
        //用于查询上一个结点
        ArrayList<TreeNode> heap = new ArrayList<>();
        int index = 0;

        for( int i = 1; i < nums.length; i++ ){
            //将上一次循环创建的节点压入栈中
            heap.add(parent);
            index++;

            //判断当前的值是不是比父节点大,小就直接进行创建,大就进行else
            if( parent.val > nums[i] ){

                parent.right = new TreeNode( nums[i] );
                parent = parent.right;

            }else{

                //弹栈,寻找合适的节点,注意判断栈内是不是空了
                while( heap.size() > 0 && heap.get( index-1 ).val < nums[i] ){
                    heap.remove( index-1 );
                    index--;
                }

                TreeNode temp = new TreeNode(nums[i] );

                //如果栈空了的话就代表当前树中所有的值都没接下来的这个大,就把这个值创建成根节点,原来的树变为左子树
                if( heap.size() == 0 ){

                    temp.left = root;
                    root = temp;
                    parent = root;
                //没空就进行交换,得到结果
                }else{

                    temp.left = heap.get( heap.size()-1 ).right;
                    heap.get( heap.size()-1 ).right = temp;
                    parent =  temp;
                }
            }

        }

        return root;
    }
    
    //输出栈内的值
    public static void pt(ArrayList<TreeNode> heap){

        for ( int i = 0; i < heap.size(); i++ ){
            System.out.print( heap.get(i).val + "\t" );
        }
        System.out.println();
    }

    //中根遍历,递归算法
    public static void ZHONGGEN(TreeNode root){
        if( root != null ){
            ZHONGGEN( root.left );
            System.out.print( root.val + "\t");
            ZHONGGEN( root.right );

        }

    }

    //先根遍历,递归算法
    public static void XIANGEN(TreeNode root){
        if( root != null ){
            System.out.print( root.val + "\t");
            XIANGEN( root.left );
            XIANGEN( root.right );

        }

    }
}
//Definition for a binary tree node.
class TreeNode {

    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}


  1. Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/maximum-binary-tree
    copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎

Published 25 original articles · won praise 0 · Views 110

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/103466266