[leetcode] Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:越狱铃声软件 编辑:程序博客网 时间:2024/06/07 00:54
From : https://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.
/** * 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* buildTree(vector<int>& inorder, vector<int>& postorder) { return build(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1); } TreeNode* build(vector<int>& in, vector<int>& post, int si, int ei, int sp, int ep) { if(si>ei || sp>ep) return NULL; if(si==ei) return new TreeNode(in[si]); int rv=post[ep], inIdx=si; while(in[inIdx]!=rv) inIdx++; int postIdx=ep-ei+inIdx; TreeNode *root = new TreeNode(rv); root->left = build(in, post, si, inIdx-1, sp, postIdx-1); root->right = build(in, post, inIdx+1, ei, postIdx, ep-1); return root; }};
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: 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 110: 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
- BZOJ3190 [JLOI2013]赛车(单调栈+半平面交)
- 黑马程序员-堆与栈
- The Skyline Problem
- RS485通讯协议的应用
- 汇编语言基础4
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- C和C++程序员面试秘籍
- MySQL按照汉字拼音字母排序
- 求一个数据结构C语言 关于关键字统计源代码
- 模板-高精度A+B,A-B
- Single Number
- SeqList.
- nefu1009消灭兔子 优先队列
- 苹果开发 笔记(38)UIImagePickerController