码迷,mamicode.com
首页 > 其他好文 > 详细

Construct Binary Tree from Inorder and Postorder Traversal

时间:2014-08-17 01:03:21      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   strong   ar   div   

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

 1 class Solution {
 2 public:
 3     TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
 4         return get_Tree(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
 5     }
 6     TreeNode * get_Tree(vector<int>::iterator in_l, vector<int>::iterator in_r, vector<int>::iterator post_l,vector<int>::iterator post_r){
 7       if(in_l >= in_r || post_l >= post_r) return NULL;
 8       TreeNode * root = new TreeNode(*prev(post_r));
 9       vector<int>::iterator loca = find(in_l,in_r,*prev(post_r));
10       root->left = get_Tree(in_l,loca,post_l,next(post_l,distance(in_l,loca)));
11       root->right = get_Tree(next(loca),in_r,next(post_l,distance(in_l,loca)),prev(post_r));
12       return root;
13     }
14 };

 

Construct Binary Tree from Inorder and Postorder Traversal,布布扣,bubuko.com

Construct Binary Tree from Inorder and Postorder Traversal

标签:style   blog   color   os   io   strong   ar   div   

原文地址:http://www.cnblogs.com/Kai-Xing/p/3917146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!