[leetcode]Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:巢湖安广网络客服电话 编辑:程序博客网 时间:2024/05/22 07:07
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 binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int findInVector(vector<int> vec, int val){ for(int i = 0; i < vec.size(); i++){ if(vec[i] == val) return i; } return -1; } TreeNode *build(int is, int ps, int N, vector<int> &inorder, vector<int> &postorder){ if(N == 0) return NULL; int rootvalue = postorder[ps+N-1]; TreeNode *root = new TreeNode(rootvalue); if(N == 1) return root; int in_index = findInVector(inorder, rootvalue); int left_len = in_index - is; int right_len = N - 1 - left_len; root -> left = build(is, ps, left_len, inorder, postorder); root -> right = build(is+1+left_len, ps+left_len, right_len, inorder, postorder); return root; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { // Start typing your C/C++ solution below // DO NOT write int main() function return build(0, 0, inorder.size(), inorder, postorder); }};
- *(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
- 查看清理matlab内存相关命令
- RAID 和 RAID卡
- 异常小结
- C/C++刁钻问题各个击破之细说sizeof
- Linux command: save the output of a command to a file
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- ltp中networkstress 问题
- ios 从网络下载图片,保存,并用imageview从保存中显示
- JDK环境变量配置的标准化步骤
- struts2文件下载
- spring的security 的api地址
- [leetcode]Maximum Depth of Binary Tree
- Jquery3_控制Dom元素1
- LINUX对音频编程的方法概述