[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:淘宝 买摩托车 编辑:程序博客网 时间:2024/05/20 13:16
106. 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.
/** * 和之前使用中序和前序遍历重建的题一样,可以直接使用索引,避免反复创建vector,这里就省略不实现了... *//** * 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) { if(inorder.empty() || postorder.empty()) return NULL; TreeNode* root = new TreeNode(*(postorder.end()-1)); int i = 0; for(; i < inorder.size(); ++i){ if(inorder[i] == root->val) break; } if(i > 0){ vector<int> in_left = vector<int>(inorder.begin(), inorder.begin()+i); vector<int> post_left = vector<int>(postorder.begin(), postorder.begin()+i); root->left = buildTree(in_left, post_left); } if(i < inorder.size()-1){ vector<int> in_right = vector<int>(inorder.begin()+i+1, inorder.end()); vector<int> post_right = vector<int>(postorder.begin()+i, postorder.end()-1); root->right = buildTree(in_right, post_right); } return root; }};
阅读全文
0 0
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-106.Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode - 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode-106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- 关于VS2017编辑C++出现的各种问题
- Markdown 语法
- R-学习记录-数组
- DataSnap——利用TParams进行多表事务更新
- Windows下安装.whl
- [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal
- Java正则表达式
- Android基础类之BaseAdapter
- zzuli-2175: GJJ的日常之再游戏
- MySQL修改表的默认字符集和修改表字段的默认字符集
- malloc(),free()与realloc()
- 小项目练手-Day1
- Deep Learning(深度学习)之(一)特征以及训练方法
- poj kmp入门几题 mark