Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

77%
+5 −0
Challenges Given the preorder and the inorder of a tree, output the postorder

Definitions A binary tree is either a null (leaf), or an object (node). A node contains a value (non-negative integer) and two pointers (left and right) to two separate binary trees. A binary tre...

2 answers  ·  posted 4y ago by Hakerh400‭  ·  last activity 3y ago by radarek‭

Question code-golf
#2: Post edited by user avatar Hakerh400‭ · 2020-12-02T03:10:19Z (about 4 years ago)
  • ### Definitions
  • A binary tree is either a `null` (leaf), or an object (node). A node contains a value (non-negative integer) and two pointers (left and right) to two separate binary trees.
  • A binary tree can be traversed in many different ways. Here we define **preorder**, **inorder** and **postorder**. We define them recursively using a pseudocode:
  • preorder(tree):
  • if tree is not a leaf:
  • visit(tree.value)
  • preorder(tree.left)
  • preorder(tree.right)
  • inorder(tree):
  • if tree is not a leaf:
  • inorder(tree.left)
  • visit(tree.value)
  • inorder(tree.right)
  • postorder(tree):
  • if tree is not a leaf:
  • postorder(tree.left)
  • postorder(tree.right)
  • visit(tree.value)
  • Each algorithm sequentially visits all nodes of the given tree.
  • ### Task
  • Given the sequence of nodes that is obtained by applying the preorder algorithm to a binary tree and given the sequence of nodes that is obtained by applying the inorder algorithm to the same tree, output the sequence of nodes that would be obtained by applying the postorder algorithm to the same tree.
  • ### Input
  • Two arrays of integers. The first one corresponds to the preorder and the second one corresponds to the inorder. Both arrays contain the same number of elements. Each element in an array is unique (there are no two distinct nodes with the same value).
  • ### Output
  • A single array of integers corresponding to the postorder.
  • ### Test cases
  • Input: [[], []]
  • Output: []
  • Input: [[0], [0]]
  • Output: [0]
  • Input: [[0, 1], [1, 0]]
  • Output: [1, 0]
  • Input: [[0, 1], [0, 1]]
  • Output: [1, 0]
  • Input: [[0, 1, 2],
  • [1, 0, 2]]
  • Output: [1, 2, 0]
  • Input: [[0, 1, 2, 3, 4],
  • [1, 2, 0, 4, 3]]
  • Output: [2, 1, 4, 3, 0]
  • Input: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
  • [4, 3, 2, 1, 7, 6, 8, 5, 9, 10, 0, 12, 14, 13, 15, 11, 18, 17, 16, 19, 20]]
  • Output: [4, 3, 2, 7, 8, 6, 10, 9, 5, 1, 14, 15, 13, 12, 18, 17, 20, 19, 16, 11, 0]
  • ### Scoring
  • The shortest program in each language wins.
  • ### Definitions
  • A binary tree is either a `null` (leaf), or an object (node). A node contains a value (non-negative integer) and two pointers (left and right) to two separate binary trees.
  • A binary tree can be traversed in many different ways. Here we define **preorder**, **inorder** and **postorder**. We define them recursively using a pseudocode:
  • preorder(tree):
  • if tree is not a leaf:
  • visit(tree.value)
  • preorder(tree.left)
  • preorder(tree.right)
  • inorder(tree):
  • if tree is not a leaf:
  • inorder(tree.left)
  • visit(tree.value)
  • inorder(tree.right)
  • postorder(tree):
  • if tree is not a leaf:
  • postorder(tree.left)
  • postorder(tree.right)
  • visit(tree.value)
  • Each algorithm sequentially visits all nodes of the given tree.
  • ### Task
  • Given the sequence of nodes that is obtained by applying the preorder algorithm to a binary tree and given the sequence of nodes that is obtained by applying the inorder algorithm to the same tree, output the sequence of nodes that would be obtained by applying the postorder algorithm to the same tree.
  • ### Input
  • Two arrays of integers. The first one corresponds to the preorder and the second one corresponds to the inorder. Both arrays contain the same number of elements. Each element in an array is unique (there are no two distinct nodes with the same value).
  • ### Output
  • A single array of integers corresponding to the postorder.
  • ### Test cases
  • Input: [[], []]
  • Output: []
  • Input: [[0], [0]]
  • Output: [0]
  • Input: [[0, 1], [1, 0]]
  • Output: [1, 0]
  • Input: [[0, 1], [0, 1]]
  • Output: [1, 0]
  • Input: [[0, 1, 2],
  • [1, 0, 2]]
  • Output: [1, 2, 0]
  • Input: [[0, 1, 2, 3, 4],
  • [1, 2, 0, 4, 3]]
  • Output: [2, 1, 4, 3, 0]
  • Input: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
  • [4, 3, 2, 1, 7, 6, 8, 5, 9, 10, 0, 12, 14, 13, 15, 11, 18, 17, 16, 19, 20]]
  • Output: [4, 3, 2, 7, 8, 6, 10, 9, 5, 1, 14, 15, 13, 12, 18, 17, 20, 19, 16, 11, 0]
  • ### Reference implementation
  • [Try it online!](https://tio.run/##lZRLc5swEMfv@RTbk6FRGMtxnh43p/aYHnJkOGhsYdMQICDSZDL@7Ok@BDY06aQXENr//vahFb/Mk2lWdVa5k6Jc27dVWTQOTNPY2sESavvYZrUNJrIzCRdekaIxqGqrICvKEJbf4PUIQGy3aCvaPF8c9Vu5aQhnOiEg2bV1ASY2UW6LjdvCCehkgbbdgV9tVxQoVRBFkak3TbgniOLB3Ns7Z1b3P2rzYL14pOy0aVsQzr1UtkyphOUSJrTpsrKYwA1uXUMaT5M4jXWScB57740t0FkY6JjCjTcDb0amqvKXgArvErh@T0B8L@j4vhcxRlDcOe5D14kufkNVYgbxsOS@3i7f39sst4EOR7X79tBJBIwKFwOBRH8yeUJVknpUf22bNqdTRGFU2GcXoHgEeV2XhWVKa3c8P@TTg7I0IEGfGu9wMt0QUGd12Jv73jCx54A0I6rKKugzgMPa8Pww/NgLk3RZQVvyvetMHtc222DUXUbwkfEqavJsRd0NfdzdaGI3fkZopL7ub4hUjNXix2Gt07Cr8FZ4ctHIsZN9YdlgFDAVDEOaZpulruuBGLNibZ/RTJF4/TM9OCnPFxGhT/SQndvUeW@pdaoEOQhSZ5vtUCbEY@hx3VT7FmMKyi9fMpuvceAUcH8oYvKBjeN4419/h2rQa7NvsuHe0k3q2xsn76eF/xUfssJ7RNdevW@Z7S2G5mGckufiHytAPb2kBt@eUOYAm7NbvMkhRGtrqztXZyv3/bE1eZAGMc5ZnIT8WBx9LKN5xEcoz38qFf5WUaYViF4Wn3Hh9@ddFMwSxVpaeb/ZZ10VnCqYC2DGjDluMWbGijkr/gem4EzBuYILBZcKrtBEZrRrIqJCo0SjRqNIo0qjTKNuxu2VgBL8gkGXTPSgqefMPevMsy8967xnhWrPklzOBXHFPH2QyKmHeshsKhCG@VN4@wM)
  • ### Scoring
  • The shortest program in each language wins.
#1: Initial revision by user avatar Hakerh400‭ · 2020-11-30T13:53:30Z (about 4 years ago)
Given the preorder and the inorder of a tree, output the postorder
### Definitions

A binary tree is either a `null` (leaf), or an object (node). A node contains a value (non-negative integer) and two pointers (left and right) to two separate binary trees.

A binary tree can be traversed in many different ways. Here we define **preorder**, **inorder** and **postorder**. We define them recursively using a pseudocode:

    preorder(tree):
      if tree is not a leaf:
        visit(tree.value)
        preorder(tree.left)
        preorder(tree.right)
    
    inorder(tree):
      if tree is not a leaf:
        inorder(tree.left)
        visit(tree.value)
        inorder(tree.right)
    
    postorder(tree):
      if tree is not a leaf:
        postorder(tree.left)
        postorder(tree.right)
        visit(tree.value)

Each algorithm sequentially visits all nodes of the given tree.

### Task

Given the sequence of nodes that is obtained by applying the preorder algorithm to a binary tree and given the sequence of nodes that is obtained by applying the inorder algorithm to the same tree, output the sequence of nodes that would be obtained by applying the postorder algorithm to the same tree.

### Input

Two arrays of integers. The first one corresponds to the preorder and the second one corresponds to the inorder. Both arrays contain the same number of elements. Each element in an array is unique (there are no two distinct nodes with the same value).

### Output

A single array of integers corresponding to the postorder.

### Test cases

    Input:  [[], []]
    Output: []
    
    Input:  [[0], [0]]
    Output: [0]
    
    Input:  [[0, 1], [1, 0]]
    Output: [1, 0]
    
    Input:  [[0, 1], [0, 1]]
    Output: [1, 0]
    
    Input:  [[0, 1, 2],
             [1, 0, 2]]
    Output: [1, 2, 0]
    
    Input:  [[0, 1, 2, 3, 4],
             [1, 2, 0, 4, 3]]
    Output: [2, 1, 4, 3, 0]
    
    Input:  [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
             [4, 3, 2, 1, 7, 6, 8, 5, 9, 10, 0, 12, 14, 13, 15, 11, 18, 17, 16, 19, 20]]
    Output: [4, 3, 2, 7, 8, 6, 10, 9, 5, 1, 14, 15, 13, 12, 18, 17, 20, 19, 16, 11, 0]

### Scoring

The shortest program in each language wins.