Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:iphone4s网络不可用 编辑:程序博客网 时间:2024/06/06 00:20
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.empty() || inorder.empty()) return NULL;return buildTree(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); }TreeNode *buildTree(const vector<int> &preorder,int preLeft,int preRight,const vector<int> &inorder,int inLeft,int inRight){if(preLeft>preRight || inLeft>inRight) return NULL;//根节点TreeNode *root=new TreeNode(preorder[preLeft]);//计算左子树结点数int i=0;for(i=inLeft;i<=inRight;i++){if(inorder[i]==preorder[preLeft]){break;}}const int leftSubTree=i-inLeft;root->left=buildTree(preorder,preLeft+1,preLeft+leftSubTree, inorder,inLeft,i-1);root->right=buildTree(preorder,preLeft+leftSubTree+1,preRight, inorder,i+1,inRight);return root;}};
0 0
- *(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
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- 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
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- form表达提交
- Cocos2d-x3.0 文件处理
- 第八周作业
- Matlab 高斯分布绘图
- 多线程的 匿名实现方法
- Construct Binary Tree from Preorder and Inorder Traversal
- junit annotation
- UVa 10152 - ShellSort
- CXF学习03---WSDL详解
- 最近走人有点多啊。
- 编程精华资源大汇总
- Android Activity切换动画 左右滑进滑出
- 多进程、多线程、多核CPU——(I)
- 三.Python数据类型の(二)序列类型(元组)