leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
2014-07-09 11:53
399 查看
题目:
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)的首个元素([b]PreSequence[0])为二叉树的根(root), 然后在中序序列(InSequence)中查找此根(root), [/b]
b、根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列, 后边的序列为根的右子树的中序遍历序列。
c、设在中序遍历序列[b](InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的 左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列 为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。[/b]
实现:
实现一:
实现二:
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)的首个元素([b]PreSequence[0])为二叉树的根(root), 然后在中序序列(InSequence)中查找此根(root), [/b]
b、根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列, 后边的序列为根的右子树的中序遍历序列。
c、设在中序遍历序列[b](InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的 左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列 为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。[/b]
实现:
实现一:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *root=NULL; creatTree(&root,preorder.begin(),preorder.end(),inorder.begin(),inorder.end()); return root; } private: //双指针(TreeNode **t)实现构建二叉树 void creatTree(TreeNode **t,vector<int>::iterator pre_beg,vector<int>::iterator pre_end,vector<int>::iterator in_beg,vector<int>::iterator in_end) { if(pre_beg==pre_end) //空树 { (*t)=NULL; return; } (*t)=new TreeNode(*pre_beg); vector<int>::iterator inRootPos=find(in_beg,in_end,(*t)->val);//中序遍历中找到根节点,返回迭代指针 int leftlen=distance(in_beg,inRootPos);//中序遍历起点指针与找到的根节点指针的距离 creatTree(&((*t)->left),next(pre_beg),next(pre_beg,leftlen+1),in_beg,inRootPos);//递归构建左子数 creatTree(&((*t)->right),next(pre_beg,leftlen+1),pre_end,next(inRootPos),in_end);//递归构建右子树 } };
实现二:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { TreeNode *root=NULL; creatTree(root,preorder.begin(),preorder.end(),inorder.begin(),inorder.end()); return root; } private: //指针引用(TreeNode *&t)实现构建二叉树 void creatTree(TreeNode *&t,vector<int>::iterator pre_beg,vector<int>::iterator pre_end,vector<int>::iterator in_beg,vector<int>::iterator in_end) { if(pre_beg==pre_end) //空树 { t=NULL; return; } t=new TreeNode(*pre_beg); vector<int>::iterator result=find(in_beg,in_end,t->val);//中序遍历中找到根节点,返回迭代指针 int len=distance(in_beg,result);//中序遍历起点指针与找到的根节点指针的距离 creatTree(t->left,pre_beg+1,pre_beg+len+1,in_beg,result);//递归构建左子数 creatTree(t->right,pre_beg+len+1,pre_end,result+1,in_end);//递归构建右子树 } };
相关文章推荐
- leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
- [LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode题解-105-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal(根据前序、中序遍历确定一棵二叉树 C语言)
- LeetCode题解:Construct Binary Tree from Preorder and Inorder Traversal
- leetcode题解-105 && 106. Construct Binary Tree from Preorder (PostOrder) and Inorder Traversal
- leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
- 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
- LeetCode题解:Construct Binary Tree from Preorder and Inorder Traversal
- 根据前序和中序序列重建二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode题解: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal (中序和后序建树,在题目给的函数中完成)【leetcode】
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [leetCode] Construct Binary Tree from Preorder and Inorder Traversal