根据后序集合找到根,再根据根,在中序集合中找到索引号,采用递归的构建子树的方法
/** * 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[] inorder, int[] postorder) { return gT(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); } private TreeNode gT(int[] inorder, int inStart, int inEnd, int[] postorder, int pStart, int pEnd){ if(inorder==null || postorder==null || inorder.length==0 || postorder.length==0 || inStart>inEnd || pStart>pEnd){ return null; } int childRootVal = postorder[pEnd]; TreeNode childRoot = new TreeNode(childRootVal); int childRootInOrderIndex = -1; for(int i=inStart; i<=inEnd; i++){ if(inorder[i]==childRootVal){ childRootInOrderIndex=i; break; } } if(childRootInOrderIndex==-1){ return null; } int duration = childRootInOrderIndex-inStart; childRoot.left=gT(inorder, inStart, childRootInOrderIndex - 1, postorder, pStart, pStart + duration - 1); childRoot.right=gT(inorder, childRootInOrderIndex+1,inEnd, postorder, pStart+duration, pEnd-1); return childRoot; } }
相关推荐
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
通过LeetCode题解的这个文件“js-leetcode题解之107-binary-tree-level-order-traversal-ii.js”,我们可以学习如何使用JavaScript编写二叉树层序遍历的变种,以及如何处理和操作队列数据结构。
105.construct_binary_tree_from_preorder_and_inorder_traversal从前序
2. 在LeetCode平台上,问题编号107的题目是“Binary Tree Level Order Traversal II”,它要求对给定的二叉树进行反向层级遍历,即从最后一层到第一层的顺序输出节点值。 3. 反向层级遍历算法可以通过先进行正常的...
在解决LeetCode上的0094号问题“Binary Tree Inorder Traversal”时,C语言是实现算法的常用语言之一。该题目要求设计一个算法来遍历二叉树,并按照中序遍历的方式返回遍历的结果。这不仅需要理解中序遍历的原理,还...
Javascript 的解决方案Leetcode Problems and interview problems in Javascript10 Regular Expresion Matching.js100 Same Tree.js101 Symmetric Tree.js102 Binary Tree Level Order Traversal.js103 Binary Tree ...
在LeetCode上,问题编号94的题目“Binary Tree Inorder Traversal”就是要求对给定的二叉树进行中序遍历,并返回遍历的结果。给定的二叉树用JavaScript对象来表示,每个节点的值为一个数字,节点结构包含值、左子...
leetcode是全球知名的在线编程平台,提供各种编程题目的解法,供程序员学习和练习。本文将详细解读如何使用Python解决leetcode上的“二叉树的中序遍历”问题,题目编号为094。该题目的核心是要求开发者编写代码,对...
root.right = buildTree(inorder[inorder_index + 1:], postorder) root.left = buildTree(inorder[:inorder_index], postorder) return root ``` 利用上面的递归函数,我们可以将给定的中序和后序遍历结果转化...
LeetCode是一个著名的在线编程平台,它为用户提供了大量的编程题目,用于练习和提升算法与编程技能。这些题目涵盖了各种难度等级,适合不同水平的开发者练习。通过解决这些题目,开发者可以为技术面试做好准备,也...
由于LeetCode平台提供了一个公共的编程问题库供开发者练习和提升编程能力,因此我们选择了其中的题目编号102,即"Binary Tree Level Order Traversal"(二叉树的层序遍历)。为了解决这个问题,我们通常需要掌握队列...
今天我们将详细讨论Python实现LeetCode第103题——二叉树的锯齿形层序遍历(Binary Tree Zigzag Level Order Traversal)的解决方案。 二叉树的锯齿形层序遍历要求我们以不同于传统层序遍历的方式输出树节点值,即...
js-leetcode题解之106-construct-binary-tree-from-inorder LeetCode第106题要求我们通过中序和后序遍历结果来重构二叉树。在JavaScript中,这是一个常见的算法问题,涉及到树的构造和递归的使用。 首先,需要理解...
在JavaScript中实现二叉树后序遍历,是众多算法练习者在LeetCode上常遇到的题目之一。题目的主要任务是遍历给定的二叉树,并按照后序遍历的规则输出节点的值。后序遍历,也就是先遍历左子树,再遍历右子树,最后访问...
java java_leetcode-114-flatten-binary-tree-to-linked-list
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
94.Binary_Tree_Inorder_Traversal二叉树的中序遍历【LeetCode单题讲解系列】
145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】