递归思想,找到根节点,问题便分解为左子树与右子树
leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
/**
* 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) {
int size = inorder.length;
if(size==0) return null;
return buildTreefromin_post(inorder, 0, size-1, postorder, 0, size-1);
}
private TreeNode buildTreefromin_post(int[] inorder, int l_in, int r_in,
int[] postorder, int l_p, int r_p){
TreeNode root = new TreeNode(0)

这篇博客利用递归思想探讨了如何根据中根序遍历和后根序遍历,以及先根序遍历和中根序遍历来构造二叉树。主要介绍了LeetCode上的106题和105题,这两道题目分别要求从这两种遍历顺序中重建二叉树。
最低0.47元/天 解锁文章
6万+

被折叠的 条评论
为什么被折叠?



