from Balanced_Trees.Binary_Search_Trees.Node import Node
from Stacks.Stack_Implementation import Stack
from Queue import Queue

root = Node(10)
root.leftChild = Node(20)
root.rightChild = Node(30)
root.leftChild.leftChild = Node(40)
root.leftChild.rightChild = Node(50)
root.rightChild.leftChild = Node(60)
root.rightChild.rightChild = Node(70)


def print_reverse_level_order(root):
    if root is None:
        return None

    my_stack = Stack()
    my_queue1 = Queue()
    my_queue2 = Queue()
    my_queue1.put(root)

    while not my_queue1.empty() or not my_queue2.empty():
        while not my_queue1.empty():
            root = my_queue1.get()

            if root.rightChild:
                my_queue2.put(root.rightChild)

            if root.leftChild:
                my_queue2.put(root.leftChild)
예제 #2
0
from Balanced_Trees.Binary_Search_Trees.Node import Node

root = Node(10)
root.leftChild = Node(8)
root.rightChild = Node(11)
root.leftChild.leftChild = Node(7)
root.leftChild.rightChild = Node(9)
root.rightChild.rightChild = Node(12)

my_list = []


def in_order(root):
    helper(root, my_list)
    return my_list


def helper(root, my_list):
    if root is None:
        return
    if root:
        in_order(root.leftChild)
        my_list.append(root.data)
        in_order(root.rightChild)


original_val = root.data
k = 2
print in_order(root)[len(my_list) - k]