LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:梦里花落知多少第81节 编辑:程序博客网 时间:2024/06/16 00:40
题目
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
思路
用前序遍历和中序遍历来构建树,比较好的一点是这棵树中没有重复的数字。
根据前序遍历和中序遍历的特点,可以很容易的想到,前序遍历的第一个元素是根节点,然后在中序遍历中找到这个根节点,这样中序遍历就可以被根节点划分为左边的左子树,和右边的右子树。
再根据上面的过程用递归的方法来分别构造左子树和右子树,就可以完成。
需要注意的是,前序序列和后序序列的边界值,以及大小要相等。
当两个序列大小不等或者等于零时,返回空。
代码
/** * 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) { if(preorder.size() != inorder.size() || preorder.size() == 0) return NULL; TreeNode* root = build(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); return root; } TreeNode* build(vector<int>& preorder,int prestart,int preend, vector<int>& inorder,int instart,int inend) { if(instart>inend || prestart>preend) return NULL; TreeNode* root = new TreeNode(preorder[prestart]); int in_i=0; // 中序遍历中根节点位置 for(int i = instart; i <= inend;i++) { if(inorder[i] == preorder[prestart]) { in_i = i; break; } } int len = in_i-instart; root->left = build(preorder,prestart+1,prestart+len,inorder,instart,in_i-1); root->right = build(preorder,prestart+len+1,preend,inorder,in_i+1,inend); return root; }};
阅读全文
0 0
- 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
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- 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
- [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
- Mac下apache支持cgi运行python3程序
- InterlockedIncrement()函数的作用
- 关于 fabric-node-sdk
- 解决ImportError: No module named zlib问题
- 使用Log4j记录日志
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- GSM HACK的另一种方法:RTL-SDR
- leetcode Swap Nodes in Pairs(Java)
- IT人为什么难以拿到高薪?
- 在脚本(script)中判断某个命令是否存在
- oracle 查询锁和解锁
- Kafka Shell基本命令(包括topic的增删改查)
- GRID K1、K2测试的初步总结
- idea未进行快捷键配置时的快捷键