Construct Binary Tree from Preorder and Inorder Traversal, is a LeetCode problem from Tree subdomain. In this post we will see how we can solve this challenge in Swift

Problem Description

You can find the full details of the problem Construct Binary Tree from Preorder and Inorder Traversal at LeetCode

Solution

then find that one in inorder to get left and right subtrees

Time Complexity: O(nlogn), Space Complexity: O(1)

Definition for a binary tree node.

public class TreeNode {

public var val: Int

public var left: TreeNode?

public var right: TreeNode?

public init(_ val: Int) {

    self.val = val

    self.left = nil

    self.right = nil

}

}

Please check the main.swift snippet for the solution.

This solution originally posted at: Github by @soapyigu