LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
2014-08-27 21:31
513 查看
[b]LeetCode: Construct Binary Tree from Inorder and Postorder Traversal[/b]
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
地址:https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
算法:根据中序和后序构造出二叉树。细心一点应该不会错。代码:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
地址:https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
算法:根据中序和后序构造出二叉树。细心一点应该不会错。代码:
/** * 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> &inorder, vector<int> &postorder) { if(inorder.empty()) return NULL; return subBuildTree(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1); } TreeNode *subBuildTree(vector<int> &inorder, vector<int> &postorder, int inbegin, int inend, int postbegin, int postend){ TreeNode *root = new TreeNode(postorder[postend]); if(inbegin == inend && postbegin == postend){ return root; } int key = postorder[postend]; int i = inbegin; while(i <= inend && inorder[i] != key) ++i; if(i > inbegin){ root->left = subBuildTree(inorder,postorder,inbegin,i-1,postbegin,postbegin+i-1-inbegin); } if(inend > i){ root->right = subBuildTree(inorder,postorder,i+1,inend,postbegin+i-inbegin,postend-1); } return root; } };
相关文章推荐
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- functionclass[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [面试真题] LeetCode:Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode刷题系列]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode 79] 106 Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- leetcode--Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal