【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:js图片特效 编辑:程序博客网 时间:2024/05/02 01:59
问题:
由中序和后序遍历构造二叉树。
分析:
Construct Binary Tree from Preorder and Inorder Traversal
//实现
TreeNode *addNode(vector<int> &inorder, int s1, int end1, vector<int> &postorder, int s2, int end2) { if(s1 > end1 || s2 > end2) return NULL; //construct the root TreeNode *root = new TreeNode(postorder[end2]); //index_of_root: the index of root in inorder. int i = s1; for(; i <= end1; ++i) { if(inorder[i] == postorder[end2]) break; } if(i > end1) return NULL; int dist = i - s1; //construct the left branch root->left = addNode(inorder, s1, i - 1, postorder, s2, s2 + dist - 1); //construct the right branch root->right = addNode(inorder, i + 1 , end1, postorder, s2 + dist, end2 - 1); return root; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int inLen = inorder.size(); int posLen = postorder.size(); if(inLen == 0 || posLen == 0 || inLen != posLen) return NULL; TreeNode *root = addNode(inorder, 0, inLen - 1, postorder, 0, posLen - 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
- quartz
- ant 常用文件操作
- wordpress博客最近变慢之解决(fonts.google.com)
- Json
- IOS中 Block简介与用法(一)
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- oracle11.2.0.3.0下载链接
- Ajax
- BucketSort
- log4j的isDebugEnabled的作用
- Hbase配置中出现的问题总结
- latex 如何输入 双斜杠 单斜杠
- Java经典习题(3)
- Oracle-GBK和UTF8的区别,该选择哪个版本