*Leetcode_construct-binary-tree-from-preorder-and-inorder-traversal
2014-04-03 13:29
357 查看
地址:http://oj.leetcode.com/problems/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.
思路:可参考上一篇pat的解题报告(后序+中序),但思路一样,http://blog.csdn.net/flyupliu/article/details/19249315
in_left 和 in_right 都是相对中序vector invec而言的,从invec的in_left到in_right位置创建节点。
当既有左子树又有右子树时,左子树的根节点位置即为当前根节点位置pre_pos加上1. 右子树的根节点位置即为根节点当前位置pre_pos + 左子树长度( i - in_left ) + 1 .
参考代码:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
思路:可参考上一篇pat的解题报告(后序+中序),但思路一样,http://blog.csdn.net/flyupliu/article/details/19249315
in_left 和 in_right 都是相对中序vector invec而言的,从invec的in_left到in_right位置创建节点。
当既有左子树又有右子树时,左子树的根节点位置即为当前根节点位置pre_pos加上1. 右子树的根节点位置即为根节点当前位置pre_pos + 左子树长度( i - in_left ) + 1 .
参考代码:
vector<int>prevec, invec; TreeNode* Construct(int pre_pos, int in_left, int in_right) { if(pre_pos>=prevec.size() || in_left>in_right) { return NULL; } TreeNode* p = new TreeNode(prevec[pre_pos]); if(in_left==in_right) return p; int i = 0; for(; i < invec.size(); ++i) { if(prevec[pre_pos]==invec[i]) break; } //left tree null if(i == in_left) { p->right = Construct(pre_pos+1, in_left+1, in_right); } //right tree null else if(i==in_right) { p->left = Construct(pre_pos+1, in_left, in_right-1); } else { p->left = Construct(pre_pos+1, in_left, i-1); p->right = Construct(pre_pos+i-in_left+1, i+1, in_right); } return p; } class Solution { public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.empty()) return NULL; prevec = preorder; invec = inorder; return Construct(0, 0, preorder.size()-1); } };
//SECOND TRIALclass Solution {private: TreeNode *construct(int pre[], int in[], int len) { if(len<=0) return NULL; int root_val = pre[0], offset = 0; TreeNode* root = new TreeNode(root_val); while(in[offset] != root_val) ++offset; root->left = construct(pre+1, in, offset); root->right = construct(pre+offset+1, in+offset+1, len-offset-1); return root; }public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int *pre = &preorder[0], *in = &inorder[0]; return construct(pre, in, preorder.size()); }};
相关文章推荐
- 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
- leetcode第44题(construct-binary-tree-from-preorder-and-inorder-……)
- construct-binary-tree-from-preorder-and-inorder-traversal——前序和中序求二叉树
- leetcode--ConstructBinaryTreefromInorderandPostorderTraversal
- *Leetcode_construct-binary-tree-from-inorder-and-postorder-traversal
- construct-binary-tree-from-preorder-and-inorder-traversal Java code
- construct-binary-tree-from-preorder-and-inorder-traversal
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- 【construct-binary-tree-from-preorder-and-inorder-traversal】
- leetcode — construct-binary-tree-from-inorder-and-postorder-traversal
- leetcode--ConstructBinaryTreefromInorderandPostorderTraversal
- construct-binary-tree-from-preorder-and-inorder-traversal
- construct-binary-tree-from-preorder-and-inorder-traversal
- LeetCode--construct-binary-tree-from-inorder-and-postorder-traversal
- leetcode — construct-binary-tree-from-inorder-and-postorder-traversal
- construct-binary-tree-from-preorder-and-inorder-traversal