[LeetCode][105,106] Construct Binary Tree from Inorder and (Post/Pre)order Traversal
来源:互联网 发布:举报网络赌钱有奖励吗 编辑:程序博客网 时间:2024/06/07 03:48
Description:
Given (pre/post)order and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
——————————————————————————————————————————————————————————————————————————
Solution:
题意:通过前序遍历和中序遍历,或后序遍历和中序遍历,构造一棵二叉树。
思路:递归构造。
由前序遍历和中序遍历:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { this->preorder = preorder; this->inorder = inorder; return constructTree(0, 0, inorder.size() - 1); } TreeNode* constructTree(int preStart, int inStart, int inEnd) { if (preStart >= preorder.size() || inStart > inEnd) return NULL; int curIndex = 0; TreeNode* curRoot = new TreeNode(preorder[preStart]); for (int i = inStart; i <= inEnd; i++) { if (preorder[preStart] == inorder[i]) { curIndex = i; break; } } curRoot->left = constructTree(preStart + 1, inStart, curIndex - 1); curRoot->right = constructTree(preStart + curIndex - inStart + 1, curIndex + 1, inEnd); return curRoot; }private: vector<int> preorder, inorder;};
由后序遍历和中序遍历:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { this->postorder = postorder; this->inorder = inorder; return constructTree(postorder.size() - 1, 0, inorder.size() - 1); } TreeNode* constructTree(int postStart, int inStart, int inEnd) { if (postStart < 0 || inStart > inEnd) return NULL; int curIndex = 0; TreeNode* curRoot = new TreeNode(postorder[postStart]); for (curIndex = inStart; curIndex <= inEnd; curIndex++) if (postorder[postStart] == inorder[curIndex]) break; curRoot->left = constructTree(postStart - (inEnd - curIndex) - 1, inStart, curIndex - 1); curRoot->right = constructTree(postStart - 1, curIndex + 1, inEnd); return curRoot; }private: vector<int> postorder, inorder;};
阅读全文
0 0
- [LeetCode][105,106] Construct Binary Tree from Inorder and (Post/Pre)order Traversal
- [LeetCode] Construct/build binary tree from in-order and post-order/pre-order traversal
- LeetCode(105)Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal [105]
- LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode 105] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode 105]Construct Binary Tree from Preorder and Inorder Traversal
- leetcode || 105、Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105:Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal LeetCode 105
- Leetcode[105]-Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode---(105)Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode #105 Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 105: Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105: Construct Binary Tree from Preorder and Inorder Traversal
- 求旋转数组中的最小值
- Overlapping Rectangles(线段树,矩形面积并)
- 多线程的三种实现方式及比较
- 嵌入式硬件系统相关知识学习笔记
- 动态规划(0-1背包问题)---C#版
- [LeetCode][105,106] Construct Binary Tree from Inorder and (Post/Pre)order Traversal
- jq代码学习2——《锋利的JQUERY》 dom对象和jQ对象转换
- 各浏览器的用户代理字符串整理
- C语言字节对齐
- STM32-外部中断学习笔记
- LeetCode Wildcard Matching
- 前端模块化
- 9. Palindrome Number
- NVM环境配置