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

Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

时间:2015-01-30 10:36:55      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

原题地址

 

二叉树基本操作

[       ]O[              ]

[       ][              ]O

 

代码:

 1 TreeNode *restore(vector<int> &inorder, vector<int> &postorder, int ip, int pp, int len) {
 2   if (len == 0)
 3     return NULL;
 4 
 5   TreeNode *node = new TreeNode(postorder[pp + len - 1]);
 6 
 7   if (len == 1)
 8     return node;
 9 
10   int leftLen = 0;
11   while (inorder[ip + leftLen] != postorder[pp + len - 1])
12     leftLen++;
13   node->left = restore(inorder, postorder, ip, pp, leftLen);
14   node->right = restore(inorder, postorder, ip + leftLen + 1, pp + leftLen, len - leftLen - 1);
15 
16   return node;
17 }
18 
19 TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
20   return restore(inorder, postorder, 0, 0, inorder.size());
21 }

 

Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

标签:

原文地址:http://www.cnblogs.com/boring09/p/4261502.html

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