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 tree.

题意:从前序遍历和中序遍历结果重构二叉树

思路1:递归

从前序遍历结果可以得到根节点

从根节点和中序遍历结果可以得到左右子树

按上面两个规律递归就可以重构二叉树了

复杂度:时间O(n) --> 总共有n个节点要构建,空间O(log n) --> 最多需要保存前面 log n个状态,每个状态需要保存的变量是O(1)

思路2:非递归 --> todo

思路2和思路1一样,不过采用非递归的方法实现

复杂度:

vector<int> _preorder;
vector<int> _inorder;
void dfs(int pre_start, int pre_end, int in_start, int in_end, TreeNode *&root){
	if(pre_start >= pre_end)return;
	root = new TreeNode(_preorder[pre_start]);
	int i;
	for(i = in_start; i < in_end; ++i){
		if(_inorder[i] == _preorder[pre_start]) break;
	}
	dfs(pre_start + 1, pre_start + 1 + i - in_start, in_start, in_start + i, root->left);
	dfs(pre_start + 1 + i - in_start, pre_end, i + 1, in_end, root->right);
}
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
	TreeNode *root = NULL;
	_preorder = preorder, _inorder = inorder;
	dfs(0, preorder.size(), 0, inorder.size(), root);
	return root;
}

时间: 2024-10-14 03:02:18

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

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

[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 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        

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. 给出树的前序和中序遍历序列,构造二叉树,假设树唯一存在. 剑指offer面试题6,重建二叉树

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. 思路:首先根据前序遍历得到根节点,然后在中序遍历中得到根节点的位置,左边的为左子树,右边的为右子树. 然后再递归求解左子树和右子树的构造即可.代码如下: /** * Definition for a binary tree

leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 给出前序遍历和中序遍历,然后求这棵树. 很有规律.递归就可以实现. /** * Definition for a binary tree node. * public class TreeNode { * int val; *

leetcode No105. Construct Binary Tree from Preorder and Inorder Traversal

Question: Given preorder and inorder traversal of a tree, construct the binary tree. 根据树的前序遍历和中序遍历,构建二叉树 Algorithm: 前序遍历:根-左-右 中序遍历:左-根-右 举个例子 前序遍历:ABDECFG 中序遍历:DBEAFCG 1.前序遍历的第一个元素A,为根结点 2.在中序遍历找到A,则在中序遍历中A左边是左子树,右边是右子树 3.递归(左右子树同样如此) Accepted Code:

Java for 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. 解题思路一: preorder[0]为root,以此分别划分出inorderLeft.preorderLeft.inorderRight.preorderRight四个数组,然后root.left=buildTree(pre