leetcode-Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:java 7 并发编程指南 编辑:程序博客网 时间:2024/05/17 08:57
Given preorder and inorder traversal of a tree, construct the binary tree.
/** * 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 *build(vector<int>&pre,int prestart,int preend,vector<int>&in,int instart,int inend) { if(preend<prestart||inend<instart||(preend-prestart)!=(inend-instart)) return NULL; TreeNode *head = new TreeNode(pre[prestart]); int rootpos; for(int i=0;i<=inend-instart;i++) if(in[i+instart]==pre[prestart]) { rootpos = i; break; } head->left = build(pre,prestart+1,prestart+rootpos,in,instart,instart+rootpos-1); head->right = build(pre,prestart+rootpos+1,preend,in,instart+rootpos+1,inend); return head; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int presize = preorder.size(); int insize = inorder.size(); TreeNode *head = build(preorder,0,presize-1,inorder,0,insize-1); return head; }};
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
- 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
- 【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
- 为hg clone 设置http代理
- Android Looper和Handler
- APK 反编译过程中的几个错误解决方法
- 新人报到。
- 网络数据(socket)传输总结
- leetcode-Construct Binary Tree from Preorder and Inorder Traversal
- 盖茨的斯坦福演讲:心碎时不要逃跑,那将是你改变世界的时刻!
- RHEL5 5升级python版本(2.4>2.7)
- 家里看上的减肥快两千几个i去
- $()和${}和$(())和(())
- android 启动过程深入解析
- NDIS网络数据监控程序NDISMonitor(1)-----驱动程序(编译过程与源码讲解)
- HDU 1021——Fibonacci Again
- Codility5 FrogRiverOne