leetcode之Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:帝国cms 图片系统数据 编辑:程序博客网 时间:2024/05/01 13:37
原题如下:
Given preorder and inorder traversal of a tree, construct the binary tree.
这道题的思路还是比较明确的,首先从前序遍历中取根节点,然后根据根节点将中序遍历分为左子树和右子树,然后再根据中序遍历的左子树和右子树分别确定前序遍历的左子树和右子树,然后依次递归。
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {if(inorder.size() == 0 || postorder.size() == 0 || inorder.size() != postorder.size())return NULL;int n = inorder.size();return built(inorder,postorder,0,n - 1,0, n - 1); }TreeNode *built(vector<int>&inorder,vector<int>&postorder,int inStart,int inEnd,int postStart, int postEnd){if(inStart > inEnd || postStart > postEnd)return NULL;TreeNode *root = new TreeNode(postorder[postEnd]);if(inStart == inEnd)return root;int leftLen = 0;//记录左子树的长度int i = inStart;//i标记中序inorder中根的下标for(;i < inEnd; i++){if(inorder[i] == root->val)break;leftLen++;}root->left = built(inorder,postorder,inStart,i - 1,postStart,postStart + leftLen - 1);root->right = built(inorder,postorder,i + 1,inEnd,postStart + leftLen, postEnd - 1);return root;}从这道题中吸取的深刻教训是:传值和传引用的区别,在将占内存比较大的对象作为参数传递时,最好选择传引用,这样可以减少内存消耗,就因为少了&符号,程序提交一直出现Memory Limit Exceeded错误,害的我郁闷了半天。。。
从中序和后序建立二叉树的思路和这道题的思路一样,所以就不多说了,直接上代码了:
class Solution {public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { if(inorder.size() == 0 || postorder.size() == 0 || inorder.size() != postorder.size())return NULL;int n = inorder.size();return built(inorder,postorder,0,n - 1,0, n - 1); }TreeNode *built(vector<int>&inorder,vector<int>&postorder,int inStart,int inEnd,int postStart, int postEnd){if(inStart > inEnd || postStart > postEnd)return NULL;TreeNode *root = new TreeNode(postorder[postEnd]);if(inStart == inEnd)return root;int leftLen = 0;//记录左子树的长度int i = inStart;//i标记中序inorder中根的下标for(;i < inEnd; i++){if(inorder[i] == root->val)break;leftLen++;}root->left = built(inorder,postorder,inStart,i - 1,postStart,postStart + leftLen - 1);root->right = built(inorder,postorder,i + 1,inEnd,postStart + leftLen, postEnd - 1);return root; }};基础不牢固导致出现各种细小差错,而这些细小差错就可能导致致命错误,教训深刻!
0 0
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode之Construct Binary Tree from Preorder and Inorder Traversal
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- 【Leetcode】之Construct Binary Tree from Preorder and Inorder Traversal
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- pl sql 不安装Orcle客户端解决办法
- 理解 javascript 函数,方法,构造器
- reportviewer 应用方法
- px、dip、sp用法详细介绍
- 关于db browser的使用
- leetcode之Construct Binary Tree from Preorder and Inorder Traversal
- 游戏开发学什么?四步修炼骨灰级高手
- hdu 3791 链表实现二叉搜索树
- JstlView
- windows编程->多线程与同步
- c 访问 mysql
- Ubuntu 12.04 root用户登录设置
- Android ndk 线程回调java层方法
- POJ 3076 Sudoku 解题报告(Dancing Link)