- 浏览: 183539 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
Construct Binary Tree from Inorder and Postorder Traversa.根据先序后续构建二叉树
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
java java_leetcode题解之Construct Binary Tree from String.java
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/...
js js_leetcode题解之106-construct-binary-tree-from-inorder
python python_leetcode题解之106_Construct_Binary_Tree_from_Inorder
java java_leetcode题解之Construct String from Binary Tree.java
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
js js_leetcode题解之105-construct-binary-tree-from-preorder
python python_leetcode题解之105_Construct_Binary_Tree_from_Preorder
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
Inorder Traversal 用两个栈实现队列 232. Implement Queue using Stacks 旋转数组的最小数字 153. Find Minimum in Rotated Sorted Array 斐波那契数列 509. Fibonacci Number 跳台阶 70. Climbing Stairs 变态跳...
1. 二叉搜索树(Binary Search Tree,简称BST): 二叉搜索树是一种特殊的二叉树,其左子树上所有节点的值均小于它的根节点的值,右子树上所有节点的值均大于它的根节点的值。这种性质使得二叉搜索树在搜索、插入和...
to construct binary codes for users and items such that the preference of users over items can be accurately preserved by the Hamming distance between their respective binary codes. By using two loss ...