【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
2017-11-20 23:32
489 查看
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Note:
You may assume that duplicates do not exist in the tree.
有了先序和中序,基本思路就是递归的找左右子树的根节点,加入到树中 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *root = NULL; // 存储树的根节点 // 递归函数,pre是先序,pl和pr为左或右子树的区间,in为中序,il和ir同为左或右子树的区间,sub决定是左子树还是右子树,par为父亲结点 void fun(vector<int> &pre, int pl, int pr, vector<int> &in, int il, int ir, int sub, TreeNode *par) { if (pl <= pr) { // 左或右子树不为空 TreeNode *tmp = new TreeNode(pre[pl]); // 新建一个结点,即某个子树的根节点 if (root == NULL) { root = tmp; // 根节点 par = root; } else { // 根据sub判断添加到左子树还是右子树 if (sub == 0) par->left = tmp; if (sub == 1) par->right = tmp; } // 找到根节点在中序遍历中的位置 int ipos = il; for (int i = il; i <= ir; ++i) { if (in[i] == pre[pl]) { ipos = i; break; } } // 找到先序遍历中左右子树的分隔位置 int ppos = pl + ipos - il; // 递归构造左右子树 if (ipos > il) fun(pre, pl+1, ppos, in, il, ipos-1, 0, tmp); // 有左子树 if (ipos < ir) fun(pre, ppos+1, pr, in, ipos+1, ir, 1, tmp); // 有右子树 } } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if (preorder.size() == 0) return NULL; //TreeNode *root = NULL; fun(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1, 0, NULL); return root; } };
相关文章推荐
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium) (cpp)
- LeetCode:M-105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- 剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode——105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告
- LeetCode *** 105. Construct Binary Tree from Preorder and Inorder Traversal