leetcode---Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:c# vsa javascript 编辑:程序博客网 时间:2024/06/03 15:41
Given inorder and postorder traversal of a tree, construct the binary 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* make(vector<int>& inorder, int inLow, int inHigh, vector<int>& postorder, int posLow, int posHigh) { if(posLow > posHigh) return NULL; else if(posLow == posHigh) return new TreeNode(postorder[posHigh]); TreeNode *root = new TreeNode(postorder[posHigh]); //根 int i = inLow; //根结点在中序遍历中的下标 for(; i<=inHigh; i++) { if(inorder[i] == postorder[posHigh]) break; } int leftLen = i - inLow; root->left = make(inorder, inLow, inLow+leftLen, postorder, posLow, posLow+leftLen-1); root->right = make(inorder, inLow+leftLen+1, inHigh, postorder, posLow+leftLen, posHigh-1); return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return make(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1); }};
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
- 动画【Android】
- 1034. Head of a Gang (30)
- 2016ICPC青岛赛区网络赛 1001
- XStream: Java对象和XML之间相互转换的利器
- UML结构建模图———类或结构图
- leetcode---Construct Binary Tree from Inorder and Postorder Traversal
- HDU 5878 I Count Two Three (dfs+二分)
- 模拟-CodeForces 370B
- HDU 5878 I Count Two Three(2016亚洲区青岛站网络赛)
- 2016ICPC青岛赛区网络赛 1002
- 欢迎使用CSDN-markdown编辑器
- hdu5889 Barricade 最短路 + 最小割
- 常见错误提示
- UML结构建模图———部署图