上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
}
};
php 解法, 执行用时: 8 ms, 内存消耗: 22.3 MB, 提交时间: 2024-02-21 09:32:41
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {
public $postorder;
public $inorder;
public $postIndex;
/**
* @param Integer[] $inorder
* @param Integer[] $postorder
* @return TreeNode
*/
function buildTree($inorder, $postorder) {
$len = count($postorder);
foreach($inorder as $k => $value){
$this->inorder[$value] = $k;
}
$this->postorder = $postorder;
$this->postIndex = $len - 1;
$root = self::helper(0, $len - 1);
return $root;
}
function helper($left, $right) {
if($left > $right) return null;
$node = $this->postorder[$this->postIndex];
$root = new TreeNode($node);
$index = $this->inorder[$node];
$this->postIndex--;
// 先右后左
$root->right = self::helper($index + 1, $right);
$root->left = self::helper($left, $index - 1);
return $root;
}
}
golang 解法, 执行用时: 0 ms, 内存消耗: 3.3 MB, 提交时间: 2022-11-19 18:28:58
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(inorder []int, postorder []int) *TreeNode {
if len(postorder) == 0 {
return nil
}
root := &TreeNode{Val: postorder[len(postorder)-1]}
stack := []*TreeNode{root}
inorderIndex := len(inorder) - 1
for i := len(postorder) - 2; i >= 0; i-- {
postorderVal := postorder[i]
node := stack[len(stack)-1]
if node.Val != inorder[inorderIndex] {
node.Right = &TreeNode{Val: postorderVal}
stack = append(stack, node.Right)
} else {
for len(stack) > 0 && stack[len(stack)-1].Val == inorder[inorderIndex] {
node = stack[len(stack)-1]
stack = stack[:len(stack)-1]
inorderIndex--
}
node.Left = &TreeNode{Val: postorderVal}
stack = append(stack, node.Left)
}
}
return root
}
golang 解法, 执行用时: 4 ms, 内存消耗: 4 MB, 提交时间: 2022-11-19 18:28:36
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func buildTree(inorder []int, postorder []int) *TreeNode {
idxMap := map[int]int{}
for i, v := range inorder {
idxMap[v] = i
}
var build func(int, int) *TreeNode
build = func(inorderLeft, inorderRight int) *TreeNode {
// 无剩余节点
if inorderLeft > inorderRight {
return nil
}
// 后序遍历的末尾元素即为当前子树的根节点
val := postorder[len(postorder)-1]
postorder = postorder[:len(postorder)-1]
root := &TreeNode{Val: val}
// 根据 val 在中序遍历的位置,将中序遍历划分成左右两颗子树
// 由于我们每次都从后序遍历的末尾取元素,所以要先遍历右子树再遍历左子树
inorderRootIndex := idxMap[val]
root.Right = build(inorderRootIndex+1, inorderRight)
root.Left = build(inorderLeft, inorderRootIndex-1)
return root
}
return build(0, len(inorder)-1)
}
python3 解法, 执行用时: 32 ms, 内存消耗: 19.3 MB, 提交时间: 2022-11-19 18:28:12
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode:
def helper(in_left, in_right):
# 如果这里没有节点构造二叉树了,就结束
if in_left > in_right:
return None
# 选择 post_idx 位置的元素作为当前子树根节点
val = postorder.pop()
root = TreeNode(val)
# 根据 root 所在位置分成左右两棵子树
index = idx_map[val]
# 构造右子树
root.right = helper(index + 1, in_right)
# 构造左子树
root.left = helper(in_left, index - 1)
return root
# 建立(元素,下标)键值对的哈希表
idx_map = {val:idx for idx, val in enumerate(inorder)}
return helper(0, len(inorder) - 1)