105&106. Construct Binary Tree from Preorder and Inorder Traversal and Inorder and Preorder
题目
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
大意
给定一颗树的前序和中序序列,重建二叉树。返回根结点。
首先,注意,vector.begin()指向第一个数,end()指向的是最后一个数的后一位。
答案
1 | /** |
思路
使用递归,在已知前序和中序时
先序的第一个数一定是根节点,在中序中找相同的数,就可以把中序序列分为两半,然后记录下在中序的指针,计算左边结点的个数,然后使用递归。
106 已知中序和后序,还原二叉树
1 | /** |
Author: corn1ng
Link: https://corn1ng.github.io/2017/11/26/算法/leetcode105&106/
License: 知识共享署名-非商业性使用 4.0 国际许可协议