- 浏览: 186217 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
用后序遍历序列来确定根节点的位置,然后在中序遍历中确定左右子树,递归完成树的构造。代码如下:
Note:
You may assume that duplicates do not exist in the tree.
用后序遍历序列来确定根节点的位置,然后在中序遍历中确定左右子树,递归完成树的构造。代码如下:
/** * 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[] inorder, int[] postorder) { if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0) return null; TreeNode root = new TreeNode(postorder[postorder.length - 1]); int i; for(i = 0; i < inorder.length; i++) { if(inorder[i] == postorder[postorder.length-1]) break; } int[] lp = Arrays.copyOfRange(postorder, 0, i); int[] li = Arrays.copyOfRange(inorder, 0, i); int[] rp = Arrays.copyOfRange(postorder, i, postorder.length - 1); int[] ri = Arrays.copyOfRange(inorder, i + 1, inorder.length); root.left = buildTree(li, lp); root.right = buildTree(ri, rp); 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 ...
相关推荐
Javascript 的解决方案Leetcode Problems and interview problems in ...Construct Binary Tree from Inorder and Postorder Traversal.js107 Binary Tree Level Order Traversal II.js108 Convert Sorted Array to ...
14. **重建二叉树**(Construct Binary Tree from Inorder and Postorder Traversal / Preorder Traversal) - 知识点:二叉树,递归,序列化与反序列化 - 解题策略:根据中序和后序/前序遍历序列,通过递归构建...
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/...
106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
lru缓存leetcode LeetCode_Note leetcode 个人笔记 ...[106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted
leetcode 跳跃 Algorithm 算法题解: 包括书籍算法, 程序员算法面试指南, 还有leetcode算法题 ...construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac
Node root = buildTree(inorder, postorder, 0, inorder.length - 1); System.out.println(levelOrder(root)); } } ``` #### 三、复旦专硕2010年机试试题分析 ##### 题目描述 从文件中读取数据构建二叉树,并...
- **中序遍历(Inorder Traversal)**:先访问左子树,然后访问根节点,最后访问右子树。这种遍历方式对于二叉搜索树特别有用,因为结果是按值排序的。 - **后序遍历(Postorder Traversal)**:先访问左子树,然后访问...
2. 中序遍历(Inorder Traversal):首先递归地进行中序遍历左子树,然后访问根节点,最后递归地进行中序遍历右子树。对于二叉搜索树(BST),中序遍历能以升序访问树中的所有节点。 3. 后序遍历(Postorder ...