问题描述:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
原问题链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
问题分析
在之前的问题里讨论过根据二叉树的前序和中序遍历序列构造整棵树的过程。这里的思路和前面基本一致。唯一的变化就是,因为这里是得到后序和中序的两个序列。那么我们就应该从后序的最后面开始看起。最后面的这个点是树的根节点。然后在对应的中序序列中它将数划分成了两部分。
这里最重要的就是要理清楚节点所在的位置关系。详细的代码实现如下:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { private Map<Integer, Integer> map; public TreeNode buildTree(int[] inorder, int[] postorder) { map = buildInOrderMap(inorder); return buildTree(postorder.length - 1, 0, inorder.length - 1, postorder, inorder); } private TreeNode buildTree(int postStart, int inStart, int inEnd, int[] postorder, int[] inorder) { if(postStart < 0 || inStart > inEnd) return null; TreeNode node = new TreeNode(postorder[postStart]); int i = map.get(postorder[postStart]); node.left = buildTree(postStart - inEnd + i - 1, inStart, i - 1, postorder, inorder); node.right = buildTree(postStart - 1, i + 1, inEnd, postorder, inorder); return node; } private Map<Integer, Integer> buildInOrderMap(int[] inorder) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < inorder.length; i++) map.put(inorder[i], i); return map; } }
相关推荐
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
leetcode的题目:Balanced Binary Tree
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】
leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道吧 2017.06.12 打卡[LeetCode 2. Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡...
正确的姿势,学习的态度来刷 LeetCode:高效的代码、简洁的注释、精炼的总结。
leetcode中文版车鸟 一组用python编写的算法。 种类 冒泡排序 插入排序 归并排序 桶排序 ...binary_tree_inorder_traversal binary_tree_level_order_traversal binary_tree_level_order_traversal_I
BinaryTree.py是一个方便的工具,它可以构建和显示编码时所需的二叉树 演示 您需要在使用之前导入该类 import BinaryTree as bt 从值/对象列表或二叉树构造二叉树 t1 = bt.BinaryTree([1,2,3,4,5,'#',6,7,'#','#','#...
Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree ...
leetcode 非官方顺序leetcode题解,主要代码为Python和C++。 leetcode 第1题: leetcode 第2题: leetcode 第3题: leetcode 第4题: leetcode 第5题: leetcode 第6题: leetcode 第7题: leetcode 第9题: ...
我的个人微信公众号:Microstrong 微信公众号ID:MicrostrongAI 微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的...102. Binary Tree Leve
LeetCode::laptop:LeetCode解决方案
leetcode11 top 1. 位运算 LeetCode191 : 二进制位1的个数 LeetCode338 : 比特位运算 2. 字典树 LeetCode209 : 实现一个Trie结构 LeetCode79 : 单词搜索(判断单词是否出现在给定的网格中) LeetCode212 : 单词搜索II...
leetcode 答案 leetCode :keyboard:我的 Leetcode 解题答案
106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
Algorithm-In-Leetcode To solve algorithm problems in leetcode.com using Python, Golang and SQL It's my leetcode account: 这是我的LeetCode中文账户: 不仅仅是Algorithm,还有Database的题目也包含在内。题目...
LeetCodeLeetCode solutions(Java)树Minimum Depth of Binary Tree栈evaluate-reverse-polish-notation穷举max-points-on-a-line链表sort-list排序insertion-sort-list树binary-tree-postorder-traversal树binary-...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。...from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
lru缓存leetcode 力码 涵盖了 Geeks for Geeks 和 Leet Code 的各种问题。 LeetCode 1 : 二和 (46_Easy) LeetCode 2 : 两个数字相加 (96_Medium) LeetCode 3 : 无重复字符的最长子串 (214_Medium) LeetCode 4 : 两个...
LeetCode 101:和你一起你轻松刷题(C++)