105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意:
给定一个先序和中序遍历序列,构建一个二叉树。
注意:二叉树中元素无重复元素。
思路:
参照106. Construct Binary Tree from Inorder and Postorder Traversal构造树的思想,先序遍历中根的顺序是从前往后的,先序遍历第一个元素是根结点(k),在中序遍历序列中找值为k的下标idx,idx将中序遍历序列分成左右子树,可进行递归操作。
1 | 1 |
1 | class Solution { |