首页 > 代码库 > Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址
二叉树基本操作
[ ]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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。