LeetCode Construct Binary Tree from Inorder and Postorder Traversal
2014-07-15 18:13
387 查看
class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int ilen = inorder.size(); int plen = postorder.size(); if (ilen == 0 || plen == 0) return NULL; return dfs(inorder, postorder, 0, ilen, plen - 1, -1); } TreeNode* dfs(vector<int> &inorder, vector<int> &postorder, int is, int ie, int ps, int pe) { if (is >= ie || ps <= pe) return NULL; int rval = postorder[ps]; int iroot= is; while (iroot != ie && inorder[iroot] != rval) iroot++; int left_len = iroot - is; TreeNode* nroot = new TreeNode(rval); nroot->left = dfs(inorder, postorder, is, iroot, pe + left_len, pe); nroot->right= dfs(inorder, postorder, iroot + 1, ie, ps - 1, pe + left_len); return nroot; } };
相关文章推荐
- 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