Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
- if not root:
- return []
- s=[root]
- res=[]
- while s:
- t=s.pop()
- if t.left:
- s.append(t.left)
- if t.right:
- s.append(t.right)
- res.append(t.val)
- return res[::-1]
- Inorder:
- def InOrder(self,root):
- ans,st=[],[]
- cur=root
- while cur or st:
- if cur==None:
- cur=st.pop()
- ans.append(cur.data)
- cur=cur.right
- else:
- st.append(cur)
- cur=cur.left
- return ans
- def preorder(root):
- res,st=[],[]
- cur=root
- while st or cur:
- if cur==None:
- cur=st.pop()
- cur=cur.right
- else:
- res.append(cur.data)
- st.append(cur)
- cur=cur.left
- return res
Add Comment
Please, Sign In to add comment