Construct Binary Tree from Inorder and Postorder Traversal

Given two integer arrays representing the inorder and postorder traversal of a binary tree, construct and return the binary tree.

Constraints

  • 1 <= inorder.length <= 3000
  • postorder.length == inorder.length
  • -3000 <= inorder[i], postorder[i] <= 3000
  • inorder and postorder consist of unique values.
  • Each value of postorder also appears in inorder.
  • inorder is guaranteed to be the inorder traversal of the tree.
  • postorder is guaranteed to be the postorder traversal of the tree.
Saving… · 12 lines
# 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 constructTreeFromInorderAndPostorderTraversal(self, inorder, postorder):



Console

Click Run to execute sample tests.