Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

tree to tuple python

def tree_to_tuple(node):
    if isinstance(node, TreeNode):

        if node.left is not None and node.right is not None:
            node_mid = node.key
            node_left = tree_to_tuple(node.left)
            node_right = tree_to_tuple(node.right)

            return (node_left, node_mid, node_right)

        elif node.left is None and node.right is None:
            return node.key

        elif node.left is None and node.right is not None:
            node_mid = node.key
            node_right = tree_to_tuple(node.right)
            node_left = None

            return (node_left, node_mid, node_right)

        elif node.left is not None and node.right is None:
            node_mid = node.key
            node_right = None
            node_left = tree_to_tuple(node.left)

            return (node_left, node_mid, node_right)

    else:
        print("It's not a tree")
Comment

PREVIOUS NEXT
Code Example
Python :: what is *args and **kwargs in django 
Python :: subset a list python 
Python :: how to take multiple line input in python 
Python :: at=error code=H10 desc="App crashed" django 
Python :: django superuser 
Python :: python extract list from string 
Python :: how to get median mode average of a python list 
Python :: python create array 
Python :: fetch data from excel in python 
Python :: python openpyxl cell width 
Python :: django url patterns static 
Python :: Iniciar servidor en Django 
Python :: hungarian algorithm python 
Python :: python str contains word 
Python :: write to csv pandas 
Python :: python how to see what pip packages are installed 
Python :: python custom exception 
Python :: represent NaN with pandas in python 
Python :: python documentation 
Python :: how to exit program in python 
Python :: how to run shell command in python 
Python :: add two column values of a datframe into one 
Python :: python check phone number 
Python :: list comprehension python if else 
Python :: pandas groupby values in list 
Python :: python create dictionary from csv 
Python :: markers seaborn 
Python :: set an index to a dataframe from another dataframe 
Python :: new line in python 
Python :: python generate pdf from template 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =