'''
Leetcode 94. Mid -sequential traversal
Given a binary tree, return to its medium -order traversal
'' '
# Definition for a binary tree node.
class time:
def __init __ (self, x):
Self.val = x
Self.left = None
Self.richt = None
class solution:
DEF InrentRTRAVERSAL (Self, ROOT: Treenode):
if root is none:
Return []
if root.left is none and root.richt is none:
Return [root.val]
stack = []# Constructing auxiliary stack
curr = root
While (Curr):
stack.append (curr)
Curr = Curr.Left
result = []
While (stack):
Curr = Stack.pop (-1)
# The current node out of the stack, indicating that all elements in the left sub -tree of the current node have been accessed
Result.append (Curr.val)
if curr.right is not none:
Curr = Curr.right
While (Curr):
stack.append (curr)
Curr = Curr.Left
Return Result
if __Name __ == "__ Main__":
root = timeode (1)
root.richt = Treenode (3)
root.left = timeode (2)
root.left.left = Treenode (4)
root.left.richt = Treenode (5)
root.richt.richt = Treenode (7)
Print (solution (). Inordertraversal (root))
# [4,2,5,1,3,7]
minimum generation tree -template scar
2023-03-14
ES