5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:mac下载苹果铃声 编辑:程序博客网 时间:2024/05/29 17:16
原题链接:https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
这道题看起来不难,但还是没有完整的思路。
我的思路:
设Pre = {1, 2, 4, 5, 3, 7}, In = {4, 2, 5, 1, 3, 7}. 首先我们从pre中知道1是根节点,那么在In里找到1, 则1左边的是它的左子树,右边是它的右子树。然后怎么做?
然后应该递归。(注意递归的思想)。
Time: O(n), Space: O(logn)
以下代码来自:http://blog.csdn.net/muscler/article/details/22907537
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if (inorder == null || preorder == null || inorder.length != preorder.length) return null; int n = inorder.length; return buildTree(preorder, inorder, 0, n - 1, 0, n - 1); } //dfs (为什么是DFS?) 根据前序遍历和中序遍历生成二叉树 public TreeNode buildTree(int[] preorder, int[] inorder, int s1, int e1, int s2, int e2) { if (s1 >= inorder.length || s2 >= inorder.length) return null; //if (s1 == e1) return new TreeNode(preorder[s1]);//不要这两行代码也可以AC //if (s1 > e1 || s2 > e2) return null; //前序遍历数组的第一个是根节点 int rootval = preorder[s1]; TreeNode root = new TreeNode(rootval); int i; //根据根节点在中序遍历数组里面找到左子树和右子数节点的个数 for (i = s2; i <= e2; i++) { if(inorder[i] == rootval) break; } int leftlength = i - s2; int rightlength = e2 - i; //dfs root.left = buildTree(preorder, inorder, s1 + 1, s1 + leftlength, s2, i - 1); root.right = buildTree(preorder, inorder, s1 + leftlength + 1, e1, i + 1, e2); return root; }}
0 0
- 5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
- 5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- 5.2.1—二叉树的构建—Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- jbpm学习--jbpm4.4 与 ssh框架的整合
- 判断一个字符串里是否有中文的方法
- (2014.07.14-2014.07.20)七天的学习小记
- JavaScript学习笔记
- 静态查找表:顺序查找、折半查找、分块查找
- 5.2.1 Construct Binary Tree from Preorder and Inorder Traversal
- 设计模式学习笔记之代理模式
- 七大内部排序算法总结(插入排序、希尔排序、冒泡排序、简单选择排序、快速排序、归并排序、堆排序)
- Android 4.0 源代码结构
- vs 2008 R2 的安装
- 开源电子商务平台
- 题目列表(图论)
- Interpreter - 解释器模式
- Delphi EnumWindows 的一个例子