LeetCodeHot100105.从前序与中序遍历序列构造二叉树
- 创业
- 2025-08-11 21:12:02

题目:给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
代码:
class Solution { private Map<Integer, Integer> indexMap; public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) { if (preorder_left > preorder_right) { return null; } // 前序遍历中的第一个节点就是根节点 int preorder_root = preorder_left; // 在中序遍历中定位根节点 int inorder_root = indexMap.get(preorder[preorder_root]); // 先把根节点建立出来 TreeNode root = new TreeNode(preorder[preorder_root]); // 得到左子树中的节点数目 int size_left_subtree = inorder_root - inorder_left; // 递归地构造左子树,并连接到根节点 // 先序遍历中「从 左边界+1 开始的 size_left_subtree」个元素就对应了中序遍历中「从 左边界 开始到 根节点定位-1」的元素 root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1); // 递归地构造右子树,并连接到根节点 // 先序遍历中「从 左边界+1+左子树节点数目 开始到 右边界」的元素就对应了中序遍历中「从 根节点定位+1 到 右边界」的元素 root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right); return root; } public TreeNode buildTree(int[] preorder, int[] inorder) { int n = preorder.length; // 构造哈希映射,帮助我们快速定位根节点 indexMap = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { indexMap.put(inorder[i], i); } return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1); } }题目:给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
中序 + 后序 构建 二叉树
class Solution { static int[] pos; static Map<Integer, Integer> map; public TreeNode buildTree(int[] inorder, int[] postorder) { int posLen = postorder.length; int inLen = inorder.length; pos = postorder; map = new HashMap<>(); for (int i = 0; i < inLen; i++) { map.put(inorder[i], i); } return buildTree(0, posLen - 1, 0, inLen - 1); } public TreeNode buildTree(int inL, int inR, int posL, int posR) { if (posL > posR || inL > inR) { return null; } int val = pos[posR]; TreeNode root = new TreeNode(val); int piv = map.get(val); int sizeL = piv - inL; int posM = posL + sizeL - 1; root.left = buildTree(inL, piv - 1, posL, posM); root.right = buildTree(piv + 1, inR, posM + 1, posR - 1); return root; } } // 代码同 中序+先序构建二叉树,只需要把先序的地方改成后序 class Solution { private Map<Integer, Integer> indexMap; public TreeNode myBuildTree(int[] postorder, int[] inorder, int postorder_left, int postorder_right, int inorder_left, int inorder_right) { if (postorder_left > postorder_right || inorder_left > inorder_right) { return null; } // 后序遍历中的最后一个节点就是根节点 int postorder_root = postorder_right; int val = postorder[postorder_root]; // 在中序遍历中定位根节点 int inorder_root = indexMap.get(val); // 先把根节点建立出来 TreeNode root = new TreeNode(postorder[postorder_root]); // 得到左子树中的节点数目 int size_left_subtree = inorder_root - inorder_left; root.left = myBuildTree(postorder, inorder, postorder_left, postorder_left + size_left_subtree - 1, inorder_left, inorder_root - 1); root.right = myBuildTree(postorder, inorder, postorder_left + size_left_subtree, postorder_right - 1, inorder_root + 1, inorder_right); return root; } public TreeNode buildTree(int[] inorder, int[] postorder ) { int n = postorder.length; // 构造哈希映射,帮助我们快速定位根节点 indexMap = new HashMap<Integer, Integer>(); for (int i = 0; i < n; i++) { indexMap.put(inorder[i], i); } return myBuildTree(postorder, inorder, 0, n - 1, 0, n - 1); } }注意:如果人家给好的函数,你把形参数位置调换了,会一直报栈溢出StackOverFlow错误!!!
LeetCodeHot100105.从前序与中序遍历序列构造二叉树由讯客互联创业栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“LeetCodeHot100105.从前序与中序遍历序列构造二叉树”
下一篇
AndroidYUV存储方式