1

I am currently practicing python and I am having an issue with binary tree. I think I am fairly good in python but I am having a hard time with binary trees.

The problem is: Problem

The first line of the function definition is given so I need to use it as is, however not sure how to calculate sum of branches.

def solution(arr):
    root = arr[0]

   for i in arr.index(i):
       left = sum(arr[i]-arr[0])
       right = sum(arr[i+1]-arr[0])

   if left > right:
       return "Left"

   elif left < right:
        return "Right"

   else:
       return ""

The error I get is

 Traceback (most recent call last):
  File "/usercode/file.py", line 36, in <module>
    test()
  File "/usercode/file.py", line 34, in test
    json.dumps(solution(*input), separators=(',', ':')))
  File "/usercode/file.py", line 5, in solution
    for i in arr.index(i):
UnboundLocalError: local variable 'i' referenced before assignment
11
  • 2
    You need to make a minimal reproducible example. Consider what @Engineero said, and note that the code you provided isn't properly indented. Commented Sep 26, 2019 at 17:36
  • 2
    You are getting that error because you haven't defined i before you use it in arr.index(i). What are you trying to do in this line? Commented Sep 26, 2019 at 17:48
  • 1
    Please fix the indentation, as Python is very sensitive to it. We can't be sure we're properly reproducing your problem if we have to fix your code ourselves before we can even run it. Commented Sep 26, 2019 at 17:53
  • 1
    I would like to use i as iterator to go over the array indexes so that odd indexes are assigned to left and even indexes are assigned to right. This is my idea but not sure how to implement it @Engineero Commented Sep 26, 2019 at 17:54
  • 1
    Copy the code from this post, paste it into a file, and try to run it. You should always do that before committing your post. Commented Sep 26, 2019 at 17:55

1 Answer 1

1

You can use recursion method to find the solution.

Following is the solution code. You can see complete code here:

class TreeNode:

    def __init__(self, lValue, lLeft=None, lRight=None):
        self.Value = lValue
        self.Left = lLeft
        self.Right = lRight


def addNode(root, lVal):
    newNode = TreeNode(lVal)
    queue = []
    queue.append(root)
    while(len(queue) > 0):
        node = queue.pop(0)
        if node.Left is None:
            node.Left = newNode
            break

        if node.Right is None:
            node.Right = newNode
            break

        queue.append(node.Left)
        queue.append(node.Right)


def createBinaryTree(lList):
    binaryTree = None
    for i in lList:
        if i is not -1:
            if binaryTree is not None:
                addNode(binaryTree, i)
            else:
                binaryTree = TreeNode(i)

    return binaryTree


def sum(node):
    if node is None:
        return 0
    lLeftVal = sum(node.Left)
    lRightVal = sum(node.Right)

    return (lLeftVal + lRightVal + node.Value)


def solution(binaryTree):
    if binaryTree == None:
        return ""

    if( sum(binaryTree.Left) > sum(binaryTree.Right) ):
        return "Left"
    else:
        return "Right"


def main():
    binaryTree = createBinaryTree([3,6,2,9,-1,10])
    print(solution(binaryTree))


main()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much!!! This sounds very complicated to me as I am still a beginner but I will go over it and try to understand Thank you again!
I already upvoted it but it doesn't show because my reputation score is not high enough. Also I have a question, would this code work if provided with an array? The problem states an array and I know that a binaryTree is a different structure. Would it still work if change all binary trees to array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.