- 浏览: 186218 次
- 性别:
- 来自: 济南
文章分类
最新评论
iven preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
给定一棵树的中序遍历和前序遍历,构造出这棵树。我们可以通过前序遍历序列的第一个元素判断出每个子树的根节点,然后在中序遍历序列找到这个根节点,假设这个根结点在i位置,那么在inorder[i]左边的就是左子树的元素,在inorder[i]右边的就是右子树的元素。用递归来完成。代码如下:
上面的代码每次递归都要在中序遍历序列中找当前的根节点,如果我们用一个哈希表来存储中序遍历序列的值和下标,那么每次查找的时间就可以缩减为O(1),这样大大优化的算法的效率。代码如下:
Note:
You may assume that duplicates do not exist in the tree.
给定一棵树的中序遍历和前序遍历,构造出这棵树。我们可以通过前序遍历序列的第一个元素判断出每个子树的根节点,然后在中序遍历序列找到这个根节点,假设这个根结点在i位置,那么在inorder[i]左边的就是左子树的元素,在inorder[i]右边的就是右子树的元素。用递归来完成。代码如下:
/** * Definition for a binary tree node. * 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(preorder == null || inorder == null || preorder.length == 0) return null; TreeNode root = new TreeNode(preorder[0]); int i; for(i = 0; i < inorder.length; i++) { if(preorder[0] == inorder[i]) break; } root.left = buildTree(Arrays.copyOfRange(preorder, 1, i + 1), Arrays.copyOfRange(inorder, 0, i)); root.right = buildTree(Arrays.copyOfRange(preorder, i + 1, preorder.length), Arrays.copyOfRange(inorder, i + 1, inorder.length)); return root; } }
上面的代码每次递归都要在中序遍历序列中找当前的根节点,如果我们用一个哈希表来存储中序遍历序列的值和下标,那么每次查找的时间就可以缩减为O(1),这样大大优化的算法的效率。代码如下:
/** * Definition for a binary tree node. * 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(preorder == null || inorder == null || preorder.length == 0) return null; HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for(int i = 0; i < inorder.length; i++) hm.put(inorder[i], i); return getTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, hm); } public TreeNode getTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, HashMap<Integer, Integer> hm) { if(preStart > preEnd || inStart > inEnd) return null; TreeNode root = new TreeNode(preorder[preStart]); int position = hm.get(root.val); int leftNums = position - inStart; root.left = getTree(preorder, preStart + 1, preStart + leftNums, inorder, inStart, position - 1, hm); root.right = getTree(preorder, preStart + leftNums + 1, preEnd, inorder, position + 1, inEnd, hm); return root; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 273Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 394Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 384Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 508Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 575Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 488Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 676Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 479The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 438Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 589Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 602Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 433All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 910Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 939Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 610Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 706Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 874Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 800You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 739For a undirected graph with tre ...
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
Javascript 的解决方案Leetcode Problems and interview problems in ...Construct Binary Tree from Preorder and Inorder Traversal.js106 Construct Binary Tree from Inorder and Postorder Traversal.js107 ...
Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳...
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
105.construct-binary-tree-from-preorder-and-inorder-traversal (从前序与中序遍历序列构造二叉树) 106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-...
[105_construct-binary-tree-from-preorder-and-inorder-traversal.cpp] [106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-...
construct-binary-tree-from-preorder-and-inorder-traversal 无官方题解 106 construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
14. **重建二叉树**(Construct Binary Tree from Inorder and Postorder Traversal / Preorder Traversal) - 知识点:二叉树,递归,序列化与反序列化 - 解题策略:根据中序和后序/前序遍历序列,通过递归构建...
- **中序遍历(Inorder Traversal)**:先访问左子树,然后访问根节点,最后访问右子树。这种遍历方式对于二叉搜索树特别有用,因为结果是按值排序的。 - **后序遍历(Postorder Traversal)**:先访问左子树,然后访问...
2. 中序遍历(Inorder Traversal):首先递归地进行中序遍历左子树,然后访问根节点,最后递归地进行中序遍历右子树。对于二叉搜索树(BST),中序遍历能以升序访问树中的所有节点。 3. 后序遍历(Postorder ...