<jupyter_start><jupyter_text>Tree 를 좌우로 invert 하여라<jupyter_code>class TreeNode:
  def __init__(self, val):
    self.val = val
    self.left = None
    self.right = None

from collections import deque

def treeLevelPrint(node):
  if node is None:
    return
  q = deque()
  q.append(node)
  while 0<len(q):
    level_count = len(q)
    for _ in range(level_count):
      crnt_node = q.popleft()
      print(crnt_node.val, end = ' ')
      if crnt_node.left:
        q.append(crnt_node.left)
      if crnt_node.right:
        q.append(crnt_node.right)
    print('')


root = TreeNode(1)
node3 = TreeNode(3)
node5 = TreeNode(5)
node7 = TreeNode(7)
node2 = TreeNode(2)
node4 = TreeNode(4)
node6 = TreeNode(6)

root.left = node3
root.right = node5
node3.left = node7
node3.right = node2
node5.left = node4
node5.right = node6

treeLevelPrint(root)
def recurInvertTree(node: TreeNode) -> TreeNode:
    if node is None:
      return
    
    tmp_node = node.left
    node.left = node.right
    node.right = tmp_node
    
    recurInvertTree(node.left)
    recurInvertTree(node.right)
    
    return node

root = recurInvertTree(root)
treeLevelPrint(root)
  def iterInvertTree(node: TreeNode) -> TreeNode:
    if node is None:
      return
    
    stack = []
    stack.append(node)
    
    while 0<len(stack):
      crnt_node = stack.pop()
      if crnt_node is None:
        continue
      tmp_node = crnt_node.left
      crnt_node.left = crnt_node.right
      crnt_node.right = tmp_node
    
      stack.append(crnt_node.right)
      stack.append(crnt_node.left)
      
    
    return root
  
root = iterInvertTree(root)  #re-invert to original 
treeLevelPrint(root)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10153482880755609, 'fraction_numerical': 0.01475796930342385, 'mean_word_length': 2.775055679287305, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '86223', 'license': 'MIT', 'n_tokens_mistral': 638, 'n_tokens_neox': 640, 'n_words': 132, 'path': 'tree/invertTree.ipynb', 'repo_name': 'NoCodeProgram/CodingTest', 'size': '1694'}