【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题:

给定二叉树的前序和中序遍历,重构这课二叉树.

分析:

前序、中序、后序都是针对于根结点而言,所以又叫作先根、中根、后根(当然不是高跟)。

前序:根  左 右

中序:左  根 右

对二叉树,我们将其进行投影,就会发现个有趣的事:

发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系。所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组。

既然中序遍历可以通过根结点将序列分为左右两个部分,那么,给出连续的根序列(由前序或后序给出),我们就可以对整个序列进行不断的划分,就可以确定左右关系,就能够构建符合这种遍历的唯一的树。

前序的 1 将中序分成两个分, 3将 4 3,5 分成了两部分,2,4,5 分别对应一个,这里就不画了。

总结来说:前序的根将中序分割成左子树和右子树,随着不断的进行,中序的划分也不断的具体,最后构造出整个树。

实现:

  TreeNode* addNode(vector<int> &preorder, int& start1, vector<int>& inorder, int start2, int end2)
{
	if(start1 >= preorder.size() || end2 < start2)
	{
	    //doesn't has left branch
		--start1;
		return NULL;
	}
	//construct the root node
	TreeNode *root = new TreeNode(preorder[start1]);

	int i;//the index of current root in inorder
	for ( i = start2; i <= end2; ++i)
	{
		if(inorder[i] == preorder[start1])
			break;
	}
    //construct left branch
	root->left = addNode(preorder, ++start1, inorder, start2, i - 1);
    // construct right branch
	root->right = addNode(preorder, ++start1, inorder, i + 1, end2);

	return root;
}

TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {

	if(preorder.size() <= 0)
		return NULL;
	int start = 0;

	TreeNode *root = addNode(preorder, start, inorder, 0, inorder.size() - 1);
	return root;
}

也可以在preorder中给定两个参数,表示起始点。

//

 TreeNode *createTree(vector<int>& preorder, int left1, int right1, vector<int>&inorder, int left2, int right2)
    {
        if(right1 < left1 || right2 < left2 )
        return NULL;
        TreeNode *root = new TreeNode(preorder[left1]);

        int i = left2;
        for(; i <= right2; ++i){
            if(inorder[i] == preorder[left1])
            break;
        }
        if(i > right2)  return NULL;
        root->left = createTree(preorder, left1 + 1, left1 + i - left2, inorder, left2, i - 1);
        root->right = createTree(preorder, left1 + i - left2 + 1, right1, inorder, i + 1, right2);
        return root;
    }
    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        if(preorder.size() == 0 || inorder.size() == 0 || preorder.size() != inorder.size())
        return NULL;
        TreeNode *root = createTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
        return root;
    }

这样的话preorder的起始结点就需要细心。代码直接在leetcode上敲的,有些排版好像不是很规整。

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

时间: 2024-08-06 02:29:25

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal的相关文章

【LeetCode】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. [解析] 题意:根据二叉树先序遍历和中序遍历的结果,构造二叉树.跟 根据中序遍历和后序遍历结果构造二叉树 类似. 先序遍历:root - left - right,中序遍历:left - root - right.

【Leetcode】【Medium】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. 解题思路: 给出一个二叉树的先序和中序遍历结果,还原这个二叉树. 对于一个二叉树: 1 / 2 3 / \ / 4 5 6 7 先序遍历结果为:1 2 4 5 3 6 7 中序遍历结果为:4 2 5 1 6 3 7 由此可以发

【树】Construct Binary Tree from Preorder and Inorder Traversal

题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后在中序序列中找到这个元素(由于题目保证没有相同的元素,因此可以唯一找到),中序序列中这个元素的左边就是左子树的中序,右边就是右子树的中序,然后根据刚才中序序列中左右子树的元素个数可以在后序序列中找到左右子树的后序序列,然后递归的求解即可 /** * Definition for a binary t

【leetcode刷题笔记】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. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

leetcode题解: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. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

[LeetCode]*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. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

Leetcode dfs Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal Total Accepted: 14824 Total Submissions: 55882My Submissions Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the

【LeetCode每天一题】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. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /

LeetCode 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. 题目标签:Array, Tree 题目给了我们preOrder 和 inOrder 两个遍历array,让我们建立二叉树.先来举一个例子,让我们看一下preOrder 和 inOrder的特性. 1 / \ 2