Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:软件系统技术说明书 编辑:程序博客网 时间:2024/06/05 06:19
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>& inorder,vector<int>& postorder,int inbegin,int inend,int postbegin,int postend) { if (inend<inbegin) return NULL; TreeNode* root=new TreeNode(postorder[postend]); if (postbegin==postend) { root->left=NULL; root->right=NULL; return root; } int i=0; for ( i=inbegin; i<=inend; i++) { if (inorder[i]==postorder[postend]) { break; } } root->left=build(inorder,postorder,inbegin,i-1,postbegin,i-1-inbegin+postbegin); root->right=build(inorder,postorder,i+1,inend,i-inbegin+postbegin,postend-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
- 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
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- c++操作符重载实现
- SAP GUI 750全新风采
- JavaScript中运算符的优先级
- 《RDF Graph Partitions: a Brief Survey》——笔记
- 未来物联网全栈开发 --JavaScript or Python?
- Construct Binary Tree from Inorder and Postorder Traversal
- Java虚拟机(三)垃圾标记算法与Java对象的生命周期
- 【C语言】、杨氏矩阵
- TensorFlow 实战资料汇总
- C#数组
- Data source rejected establishment of connection, message from server: "Too many connections"
- 网络常用入侵技术分析
- 前端开发入门提升经验整理
- VS当添加了引用但是编译时仍然提示添加引用的解决方法