Started by Bell GrantNov 3, 2021

Open
I cannot understand output order of the recursive function

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

I have got the subsequent code to assemble a binary tree from a listing. But I do not understand the order of the output. If i enter [1,2,3] the output is [2,1,3]. I notion it has to be [1,3,2] can all people help me explain it... I can not think it via.

// Definition for a binary tree node.
// class TreeNode(object):
//    def __init__(self, val=0, left=None, right=None):
//        self.val = val
//         self.left = left
//         self.right = right
class Solution(object):
    
    def sortedArrayToBST(self, nums):
   
       
        def constructBST(l,r):
            if l>r:
                return None
            mid=(l+r)//2
            
            root = TreeNode(nums[mid])
          
            root.left=constructBST(l,mid-1)
            root.right=constructBST(mid+1,r)
           
            return root
        return constructBST(0,len(nums)-1)

0 Replies

You must be Logged in to reply
Trending Technologies
15
Software39
DevOps45
Frontend Development24
Backend Development20
Server Administration17
Linux Administration24
Data Center24
Sentry24
Terraform21
Ansible46
Docker28
Penetration Testing14
Kubernetes17
NGINX19
JenkinsX17
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan