Techiio-author
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
Techiio-logo

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:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance