105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:云计算与虚拟化的区别 编辑:程序博客网 时间:2024/06/11 03:42
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
这道题是利用一棵树的先序遍历,和中序遍历重建出一颗树,LeetCode中有一道相似的题目:106. Construct Binary Tree from Inorder and Postorder Traversal。
这个题的关键是找到根节并确定其对应的左子树先序遍历区间和中序遍历区间。比如对于如下的图:
preorder : 5 3 2 4 7 6 8
inorder: 2 3 4 5 6 7 8
这个例子里,先序遍历中的5,其实就是整个树的根节点,接下来就是要确定左右子树了。从中序遍历可知,一个节点左边的部分就是这个节点左子树所有节点,右边部分就是右子树的全部节点。好在这个题规定没有重复元素,所以就方便了许多。
对于根节点5,其左子树的先序遍历为【3 2 4】中序遍历为【2 3 4】,其右子树的先序遍历为【7 6 8】中序遍历为【6 7 8】。
先考虑左子树,根节点为3, 按照上边的思路又可以将先序遍历和中序遍历的数组划分成两个部分,右子树同理。
递归出口:当数组被切割成只有一个元素时,说明这个节点是叶子节点;当数组被切割成空集,说明不存在节点,也就是NULL。
代码参考Grandyang的博客,这个博客还解释了为什么先序遍历和后序遍历不能构造二叉树的原因。
/** * 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) { return buildTree(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1); } TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder, int pleft, int pright, int ileft, int iright) { if (pleft > pright || ileft > iright) return NULL; int i = 0; for (i = ileft; i <= iright; i++) { if (preorder[pleft] == inorder[i]) break; } TreeNode* cur = new TreeNode(preorder[pleft]); cur->left = buildTree(preorder, inorder, pleft + 1, pleft + i - ileft, ileft, i - 1); cur->right = buildTree(preorder, inorder, pleft + i - ileft + 1, pright, i + 1, iright); return cur; }};
当时对
cur->left = buildTree(preorder, inorder, pleft + 1, pleft + i - ileft, ileft, i - 1); cur->right = buildTree(preorder, inorder, pleft + i - ileft + 1, pright, i + 1, iright);这两句中的pleft + i - ileft不太理解,如果大家有跟我一样的困惑,可以自己走一遍下边的这个例子,也许就明白这么做的用意了:
参考: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
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 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
- 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 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- Mac OSX终端安装主题(oh my zsh)
- Java 字符流 IO
- 485. Max Consecutive Ones
- Kafka与UnknownHostException
- C语言中的指针常量与常量指针
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- ESP8266--学习笔记(转载资料一)
- c++ 知识汇总
- GPLT L3-004. 肿瘤诊断【三维bfs求连通块】
- MongoDB相关
- swift数据持久化之属性列表,解决plist写入失败的问题
- 一、开始学习c++
- hdu 1480 钥匙计数之二
- linux 达人养成计划 II笔记