'''simple docstring'''



from __future__ import annotations

import queue






class 	UpperCamelCase__:




				def __init__(		self      :			Any				,   lowerCAmelCase      :			Optional[int]				)->					List[str]:

										"""simple docstring"""




										UpperCAmelCase					    =     data
										UpperCAmelCase					    =     None
										UpperCAmelCase					    =     None







def 				lowerCamelCase__				(	):




						'''simple docstring'''






						print('''\n********Press N to stop entering at any point of time********\n'''					)
						UpperCAmelCase					    =     input('''Enter the value of the root node: '''					).strip().lower()
						UpperCAmelCase					    =     queue.Queue()
						UpperCAmelCase					    =     TreeNode(int(__lowerCAmelCase					)					)
						q.put(__lowerCAmelCase					)
						while not q.empty():
												UpperCAmelCase					    =     q.get()
												UpperCAmelCase					    =     f"""Enter the left node of {node_found.data}: """
												UpperCAmelCase					    =     input(__lowerCAmelCase					).strip().lower() or '''n'''
												if check == "n":
																		return tree_node
												UpperCAmelCase					    =     TreeNode(int(__lowerCAmelCase					)					)
												UpperCAmelCase					    =     left_node
												q.put(__lowerCAmelCase					)
												UpperCAmelCase					    =     f"""Enter the right node of {node_found.data}: """
												UpperCAmelCase					    =     input(__lowerCAmelCase					).strip().lower() or '''n'''
												if check == "n":
																		return tree_node
												UpperCAmelCase					    =     TreeNode(int(__lowerCAmelCase					)					)
												UpperCAmelCase					    =     right_node
												q.put(__lowerCAmelCase					)
						raise


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						print(node.data				,       end=''','''					)
						pre_order(node.left					)
						pre_order(node.right					)


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						in_order(node.left					)
						print(node.data				,       end=''','''					)
						in_order(node.right					)


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						post_order(node.left					)
						post_order(node.right					)
						print(node.data				,       end=''','''					)


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						UpperCAmelCase					    =     queue.Queue()
						q.put(__lowerCAmelCase					)
						while not q.empty():
												UpperCAmelCase					    =     q.get()
												print(node_dequeued.data				,       end=''','''					)
												if node_dequeued.left:
																		q.put(node_dequeued.left					)
												if node_dequeued.right:
																		q.put(node_dequeued.right					)


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						UpperCAmelCase					    =     queue.Queue()
						q.put(__lowerCAmelCase					)
						while not q.empty():
												UpperCAmelCase					    =     []
												while not q.empty():
																		UpperCAmelCase					    =     q.get()
																		print(node_dequeued.data				,       end=''','''					)
																		if node_dequeued.left:
																								list_.append(node_dequeued.left					)
																		if node_dequeued.right:
																								list_.append(node_dequeued.right					)
												print()
												for node in list_:
																		q.put(__lowerCAmelCase					)


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     node
						while n or stack:
												while n:  # start from root node, find its left child
																		print(n.data				,       end=''','''					)
																		stack.append(__lowerCAmelCase					)
																		UpperCAmelCase					    =     n.left
												# end of while means current node doesn't have left child
												UpperCAmelCase					    =     stack.pop()
												# start to traverse its right child
												UpperCAmelCase					    =     n.right


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						UpperCAmelCase					    =     []
						UpperCAmelCase					    =     node
						while n or stack:
												while n:
																		stack.append(__lowerCAmelCase					)
																		UpperCAmelCase					    =     n.left
												UpperCAmelCase					    =     stack.pop()
												print(n.data				,       end=''','''					)
												UpperCAmelCase					    =     n.right


def 				lowerCamelCase__				(	A       :      TreeNode					):




						'''simple docstring'''






						if not isinstance(__lowerCAmelCase				,       __lowerCAmelCase					) or not node:
												return
						UpperCAmelCase    ,    UpperCAmelCase					    =     [], []
						UpperCAmelCase					    =     node
						stacka.append(__lowerCAmelCase					)
						while stacka:  # to find the reversed order of post order, store it in stack2
												UpperCAmelCase					    =     stacka.pop()
												if n.left:
																		stacka.append(n.left					)
												if n.right:
																		stacka.append(n.right					)
												stacka.append(__lowerCAmelCase					)
						while stacka:  # pop up from stack2 will be the post order
												print(stacka.pop().data				,       end=''','''					)


def 				lowerCamelCase__				(	A       :      str = ""				,       A       :      str=50				,       A       :      Dict="*"					):




						'''simple docstring'''






						if not s:
												return "\n" + width * char
						UpperCAmelCase    ,    UpperCAmelCase					    =     divmod(width - len(__lowerCAmelCase					) - 2				,       2					)
						return f"""{left * char} {s} {(left + extra) * char}"""


if __name__ == "__main__":
	import doctest

	doctest.testmod()
	print(prompt("""Binary Tree Traversals"""))

	_lowercase      :      TreeNode            =					build_tree()
	print(prompt("""Pre Order Traversal"""))
	pre_order(node)
	print(prompt() + """\n""")

	print(prompt("""In Order Traversal"""))
	in_order(node)
	print(prompt() + """\n""")

	print(prompt("""Post Order Traversal"""))
	post_order(node)
	print(prompt() + """\n""")

	print(prompt("""Level Order Traversal"""))
	level_order(node)
	print(prompt() + """\n""")

	print(prompt("""Actual Level Order Traversal"""))
	level_order_actual(node)
	print("""*""" * 50 + """\n""")

	print(prompt("""Pre Order Traversal - Iteration Version"""))
	pre_order_iter(node)
	print(prompt() + """\n""")

	print(prompt("""In Order Traversal - Iteration Version"""))
	in_order_iter(node)
	print(prompt() + """\n""")

	print(prompt("""Post Order Traversal - Iteration Version"""))
	post_order_iter(node)
	print(prompt())









'''simple docstring'''



_lowercase      :      Any            =					range(2, 20 + 1)
_lowercase      :      str            =					[10**k for k in range(ks[-1] + 1)]
_lowercase      :      dict[int, dict[int, list[list[int]]]]            =					{}


def 				lowerCamelCase__				(	A       :      int				,       A       :      str				,       A       :      List[Any]				,       A       :      Union[str, Any]					):




						'''simple docstring'''






						UpperCAmelCase					    =     sum(a_i[j] for j in range(A				,       len(A					)					)					)
						UpperCAmelCase					    =     sum(a_i[j] * base[j] for j in range(min(len(A					)				,       A					)					)					)

						UpperCAmelCase    ,    UpperCAmelCase					    =     0, 0
						UpperCAmelCase					    =     n - i

						UpperCAmelCase					    =     memo.get(A					)

						if sub_memo is not None:
												UpperCAmelCase					    =     sub_memo.get(A					)

												if jumps is not None and len(A					) > 0:
																		# find and make the largest jump without going over
																		UpperCAmelCase					    =     -1
																		for _k in range(len(A					) - 1				,       -1				,       -1					):
																								if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																														UpperCAmelCase					    =     _k
																														break

																		if max_jump >= 0:
																								UpperCAmelCase    ,    UpperCAmelCase    ,    UpperCAmelCase					    =     jumps[max_jump]
																								# since the difference between jumps is cached, add c
																								UpperCAmelCase					    =     diff + c
																								for j in range(min(A				,       len(A					)					)					):
																														UpperCAmelCase    ,    UpperCAmelCase					    =     divmod(A				,       10					)
																								if new_c > 0:
																														add(A				,       A				,       A					)

												else:
																		UpperCAmelCase					    =     []
						else:
												UpperCAmelCase					    =     {c: []}
												UpperCAmelCase					    =     sub_memo

						if dn >= max_dn or c + diff >= base[k]:
												return diff, dn

						if k > ks[0]:
												while True:
																		# keep doing smaller jumps
																		UpperCAmelCase    ,    UpperCAmelCase					    =     next_term(A				,       k - 1				,       i + dn				,       A					)
																		diff += _diff
																		dn += terms_jumped

																		if dn >= max_dn or c + diff >= base[k]:
																								break
						else:
												# would be too small a jump, just compute sequential terms instead
												UpperCAmelCase    ,    UpperCAmelCase					    =     compute(A				,       A				,       i + dn				,       A					)
												diff += _diff
												dn += terms_jumped

						UpperCAmelCase					    =     sub_memo[c]

						# keep jumps sorted by # of terms skipped
						UpperCAmelCase					    =     0
						while j < len(A					):
												if jumps[j][1] > dn:
																		break
												j += 1

						# cache the jump for this value digitsum(b) and c
						sub_memo[c].insert(A				,       (diff, dn, k)					)
						return (diff, dn)


def 				lowerCamelCase__				(	A       :      Dict				,       A       :      Optional[int]				,       A       :      List[Any]				,       A       :      int					):




						'''simple docstring'''






						if i >= n:
												return 0, i
						if k > len(A					):
												a_i.extend([0 for _ in range(k - len(A					)					)]					)

						# note: a_i -> b * 10^k + c
						# ds_b -> digitsum(b)
						# ds_c -> digitsum(c)
						UpperCAmelCase					    =     i
						UpperCAmelCase    ,    UpperCAmelCase    ,    UpperCAmelCase					    =     0, 0, 0
						for j in range(len(A					)					):
												if j >= k:
																		ds_b += a_i[j]
												else:
																		ds_c += a_i[j]

						while i < n:
												i += 1
												UpperCAmelCase					    =     ds_c + ds_b
												diff += addend
												UpperCAmelCase					    =     0
												for j in range(A					):
																		UpperCAmelCase					    =     a_i[j] + addend
																		UpperCAmelCase    ,    UpperCAmelCase					    =     divmod(A				,       10					)

																		ds_c += a_i[j]

												if addend > 0:
																		break

						if addend > 0:
												add(A				,       A				,       A					)
						return diff, i - start_i


def 				lowerCamelCase__				(	A       :      List[str]				,       A       :      Optional[int]				,       A       :      Optional[Any]					):




						'''simple docstring'''






						for j in range(A				,       len(A					)					):
												UpperCAmelCase					    =     digits[j] + addend
												if s >= 10:
																		UpperCAmelCase    ,    UpperCAmelCase					    =     divmod(A				,       10					)
																		UpperCAmelCase					    =     addend // 10 + quotient
												else:
																		UpperCAmelCase					    =     s
																		UpperCAmelCase					    =     addend // 10

												if addend == 0:
																		break

						while addend > 0:
												UpperCAmelCase    ,    UpperCAmelCase					    =     divmod(A				,       10					)
												digits.append(A					)


def 				lowerCamelCase__				(	A       :      int = 10**15					):




						'''simple docstring'''






						UpperCAmelCase					    =     [1]
						UpperCAmelCase					    =     1
						UpperCAmelCase					    =     0
						while True:
												UpperCAmelCase    ,    UpperCAmelCase					    =     next_term(A				,       20				,       i + dn				,       A					)
												dn += terms_jumped
												if dn == n - i:
																		break

						UpperCAmelCase					    =     0
						for j in range(len(A					)					):
												a_n += digits[j] * 10**j
						return a_n


if __name__ == "__main__":
	print(F"""{solution() = }""")





