题目连接:
https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
题目大意:
中文题目
AC代码:
1 TreeNode * construcuFormPrePost(vrctor<int>& pre, vector<int>&post){ 2 3 if(pre.empty()) 4 return NULL; 5 stack<TreeNode*>S; 6 auto root = new Treenode(pre[0]); 7 S.push(root); 8 for(int i = 1, j = 0; i <pre.size(); i++){ 9 auto node = new TreeNode(pre[i]); 10 while(S.top()->val == post[j])S.pop(), j++; 11 if(!S.top()->left) 12 S.top()->left = node; 13 else 14 S.top()->right = node; 15 S.push(node); 16 } 17 return root; 18 }
原文地址:https://www.cnblogs.com/letlifestop/p/11517160.html
时间: 2024-10-25 18:45:20