leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 中前序构造BST
2017-09-14 10:49
525 查看
Given preorder and inorder traversal of a tree, construct the binary tree.
本题就是根据前序遍历和中序遍历的结果还原一个二叉树。
题意很简答,就是递归实现,直接参考代码吧。
查询index部分可以使用Map做查询,建议和下一道题 leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中后序构造BST和leetcode 449. Serialize and Deserialize BST 二叉搜索树BST的序列化和反序列化一起学习。
建议和leetcode 87. Scramble String 字符串拼凑 && DFS深度优先搜索 和 leetcode 95. Unique Binary Search Trees II 递归构造所有可能的搜索二叉树BST + 卡特兰数 一起学习,因为做法类似
这道题可以和leetcode 108. Convert Sorted Array to Binary Search Tree 构建平衡二叉搜索树 + DFS 一起学习
建议和leetcode 331. Verify Preorder Serialization of a Binary Tree 二叉树的前序序列验证 和 leetcode 654. Maximum Binary Tree 构造最大二叉树 和leetcode 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化 + 深度优先遍历DFS一起学习
代码如下:
下面是C++的做法,就是利用BST二叉搜索树的中序遍历和前序遍历的性质使用DFS深度优先遍历的递归来解决这个问题
代码如下:
本题就是根据前序遍历和中序遍历的结果还原一个二叉树。
题意很简答,就是递归实现,直接参考代码吧。
查询index部分可以使用Map做查询,建议和下一道题 leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中后序构造BST和leetcode 449. Serialize and Deserialize BST 二叉搜索树BST的序列化和反序列化一起学习。
建议和leetcode 87. Scramble String 字符串拼凑 && DFS深度优先搜索 和 leetcode 95. Unique Binary Search Trees II 递归构造所有可能的搜索二叉树BST + 卡特兰数 一起学习,因为做法类似
这道题可以和leetcode 108. Convert Sorted Array to Binary Search Tree 构建平衡二叉搜索树 + DFS 一起学习
建议和leetcode 331. Verify Preorder Serialization of a Binary Tree 二叉树的前序序列验证 和 leetcode 654. Maximum Binary Tree 构造最大二叉树 和leetcode 297. Serialize and Deserialize Binary Tree 二叉树的序列化和反序列化 + 深度优先遍历DFS一起学习
代码如下:
/*class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }*/ /* * 递归生成二叉搜索树 * 总体思路计算利用前序或者后续的相关性质,然后对中序数组最分割 * */ public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder==null || preorder.length<=0 || inorder==null || inorder.length<=0 || preorder.length!=inorder.length) return null; else return getRootByRecursion(preorder,inorder,0,0,inorder.length-1); } /* * inStart和inEnd是中序结果的遍历下标,preStart是前序的遍历下标 * */ TreeNode getRootByRecursion(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) { if(inStart > inEnd) return null; else { //前序的第一个节点就是父节点 TreeNode root=new TreeNode(preorder[preStart]); int index=getIndex(inorder,preorder[preStart]); //lenLeft是左子树的元素数量 int lenLeft=(index-1)-inStart +1; //lenRight是右子树的元素数量 int lenRight=(inEnd-inStart + 1) -lenLeft -1; //递归生成左右子树 if(lenLeft>0) root.left=getRootByRecursion(preorder, inorder, preStart+1, inStart, index-1); if(lenRight>0) root.right=getRootByRecursion(preorder, inorder, preStart+lenLeft+1, index+1, inEnd); return root; } } //在中序中查找下标 int getIndex(int[] inorder, int val) { for(int i=0;i<inorder.length;i++) { if(val==inorder[i]) return i; } return -1; } }
下面是C++的做法,就是利用BST二叉搜索树的中序遍历和前序遍历的性质使用DFS深度优先遍历的递归来解决这个问题
代码如下:
#include <iostream> #include <vector> #include <algorithm> using namespace std; /* 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() <= 0) return NULL; else return getAll(preorder, inorder, 0, 0, inorder.size() - 1); } TreeNode* getAll(vector<int>& p, vector<int>& i,int pbeg,int ibeg,int iend) { if (ibeg > iend) return NULL; else { int index = getIndex(i, p[pbeg]); int leftLen = (index - 1) - ibeg + 1; int rightLen = (iend - ibeg + 1) - leftLen - 1; TreeNode* root = new TreeNode(p[pbeg]); root->left = getAll(p, i, pbeg + 1, ibeg, index - 1); root->right = getAll(p, i, pbeg + leftLen + 1, index+1, iend); return root; } } 4000 int getIndex(vector<int>& a, int target) { for (int i = 0; i < a.size(); i++) { if (a[i] == target) return i; } return 0; } };
相关文章推荐
- leetcode-java-105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode:M-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@python
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 题解
- 【C++】【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@Java解题报告
- LeetCode *** 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
- leetcode_middle_95_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 106. Construct Binary Tree from Inorder and Postorder Traversal 中后序构造BST
- leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal