Problem Solving with Algorithms

반응형

def preorder(root):
  return [root.val] + preorder(root.left) + preorder(root.right) if root else []

 

def inorder(root):
  return  inorder(root.left) + [root.val] + inorder(root.right) if root else []

 

def postorder(root):
  return  postorder(root.left) + postorder(root.right) + [root.val] if root else []

 

 

 

leetcode.com/problems/binary-tree-inorder-traversal/discuss/283746/all-dfs-traversals-preorder-inorder-postorder-in-python-in-1-line

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band