[Luffy] leetcode105. Construct binary tree from preorder and inorder traversal sequence

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

1. Description of the topic

leetcode105. Construct Binary Trees from Preorder and Inorder Traversal Sequences

Given two integer array sums  preorder , inorder where  preorder is a preorder traversal of a binary tree and is an inorder traversal inorder of the same tree, construct a binary tree and return its root node.

Example 1:

image.png

输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]
复制代码

Example 2:

输入: preorder = [-1], inorder = [-1]
输出: [-1]
复制代码

hint:

  • 1 <= preorder.length <= 3000
  • inorder.length == preorder.length
  • -3000 <= preorder[i], inorder[i] <= 3000
  • preorder and  inorder have no repeating elements
  • inorder appear in preorder
  • preorder Guaranteed to be a preorder traversal sequence of a binary tree
  • inorder Guaranteed to be an inorder traversal sequence of a binary tree

2. Thought analysis

recursion

There is no need to expand understanding, and use the semantic information of recursive functions to design programs.

First of all, we know that pre-order traversal (left and right root) and in-order traversal (left root and right) can be distinguished according to the position of the root.
The first element of pre-order traversal is that the root node
takes the first element of pre-order traversal to find the in-order The corresponding position in the traversal, then the first half is the left subtree, and the second half is the right subtree

  • Find the position of the root node in the preorder traversal
  • After finding the root node position, split the left subtree and the right subtree
  • Split out: preorder traversal of left subtree, inorder traversal of left subtree; preorder traversal of right subtree, inorder traversal of right subtree
  • create node

Example 1image.png

3. JavaScript code

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */


var buildTree = function(preorder, inorder) {
    if(preorder.length == 0) return null;
    let pos = 0;
    // 找根节点在前序遍历中的位置
    while(preorder[0] != inorder[pos]) pos++;
    // 找到根节点位置后,把左子树和右子树拆分出来
    let l_pre=[], l_in=[], r_pre=[], r_in=[]; // 左子树的前序遍历,左子树的中序遍历;右子树的前序遍历,右子树的中序遍历
    for(let i = 0; i < pos; i++) {
        l_pre.push(preorder[i + 1])
        l_in.push(inorder[i])
    }
    for(let i = pos+1; i < preorder.length; i++) {
        r_pre.push(preorder[i])
        r_in.push(inorder[i])
    }
    // 创建node
    let node = new TreeNode(preorder[0])
    // if(preorder.length == 1) return node;
    node.left = buildTree(l_pre, l_in)
    node.right = buildTree(r_pre, r_in)
    return node
};
复制代码

4. Summary

Guess you like

Origin juejin.im/post/7078098758470205471