Preamble sequence structure of the binary tree and

Topic links:

Involving knowledge: the binary tree traversal

analysis:

Binary tree preorder traversal: root -> left sub-tree -> right subtree

Binary tree traversal sequence: left subtree -> root -> right subtree

It can be seen: the first element in a preorder traversal is to access the root node, by which point they can be preorder into left and right two parts, the left part of the elements used to generate a left subtree of the binary tree, the right portion right subtree for generating a binary tree.

Similarly, the left and right portions of the two elements, the first order appear in the previous traversal of the subtree root is, it is clear that meet the definition of recursion.

code show as below:

/*
 * @lc app=leetcode.cn id=105 lang=java
 *
 * [105] 从前序与中序遍历序列构造二叉树
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private int[] preorder;
    private int[] inorder;
    private int tag;  //指向下一个要找的根节点

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        this.inorder = inorder;
        this.tag = 0;

        return generateTree(0, preorder.length - 1);
    }

    //用中序 s 到 e 的元素生成二叉树,并返回根节点
    public TreeNode generateTree(int s, int e){
        if(s > e){
            return null;
        }

        TreeNode node = null;
        for(int i = s; i <= e; ++i){
            if(inorder[i] == preorder[tag]){
                node = new TreeNode(preorder[tag++]);
                
                //首先左半边元素生成左子树
                node.left = generateTree(s, i - 1);
                
                //再生成右子树
                node.right = generateTree(i + 1, e);
                break;
            }
        }

        return node;
    }
}

Complexity analysis:

time complexity:

  • If the tree is a single branch of the tree, when the right, the height of the tree is n, in which case, in preorder traversal to the preamble and the same time to find a root complex in order traversal is O ( 1) Since the n-th node as the root node are returned, the time complexity of O (n)
  • If the tree is a relatively balanced binary tree, when, T (n) = n / 2 + 2 * T (n / 2), the master theorem too, the time complexity is O (nlogn)
  • If the tree is a single branch of the tree left when looking from the time the root node in a preorder traversal takes: T (n) = n + (n - 1) + ... + 2 + 1, the time complexity is \ (O (^ n-2) \) .

Space complexity: complexity and space about the height of the tree, in the worst case is O (n)

optimization:

You may be used HashMap correspondence between the storage and traversing subscripts elements, reduce the time used by the root node query.

class Solution {
    private int[] preorder;
    private int[] inorder;
    private int tag;
    private HashMap<Integer, Integer> map = new HashMap<>();

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        this.preorder = preorder;
        this.inorder = inorder;
        this.tag = 0;

        //将中序遍历存入哈希表
        for(int i = 0; i < inorder.length; ++i){
            map.put(inorder[i], i);
        }

        return generateTree(0, preorder.length - 1);
    }

    //用中序的 s 到 e 生成二叉树,并返回根节点
    public TreeNode generateTree(int s, int e){
        if(s > e){
            return null;
        }

        int index = map.get(preorder[tag]);
        TreeNode node = new TreeNode(preorder[tag++]);
        node.left = generateTree(s, index - 1);
        node.right = generateTree(index + 1, e);

        return node;
    }
}

Complexity analysis:

Time Complexity: simple to understand, each of the elements are to be returned as a root node, it takes O (n), the root of the query time complexity of O (1), so the total time complexity is O (n); or calculated using the above master theorem.

Space complexity: the main recursive stack memory hash table of FIG consumption and, each are O (n), the total space complexity is O (n)

Guess you like

Origin www.cnblogs.com/zcxhaha/p/11470192.html