从中序与后序遍历序列构造二叉树-LeetCode
从中序与后序遍历序列构造二叉树
🎯 问题描述(来源于LeetCode)
描述:
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
说明:
-
1 <= inorder.length <= 3000 -
postorder.length == inorder.length -
-3000 <= inorder[i], postorder[i] <= 3000 -
inorder和postorder都由 不同 的值组成 -
postorder中每一个值都在inorder中 -
inorder保证是树的中序遍历 -
postorder保证是树的后序遍历
示例: -
示例 1:
1 | 输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] |
- 示例 2:
1 | 输入:inorder = [-1], postorder = [-1] |
💻 解题思路
思路1:递归+分治
思路1:代码实现
1 | # Definition for a binary tree node. |
思路1:📊 性能分析
提交结果
- 运行时间:99ms击败35.63%
- 内存消耗:89.03MB击败36.50%
复杂度验证
- 时间复杂度:
- 空间复杂度:
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 笺札!






