Implement an iterative, in-order traversal of a given binary tree, return the list of keys of each node in the tree as it is in-order traversed.
5
/ \
3 8
/ \ \
1 4 11
In-order traversal is [1, 3, 4, 5, 8, 11]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.
class Solution(object):
def inOrder(self, root):
if not root:
return []
res = []
self.helper(root,res)
return res
def helper(self,root,res):
if not root:
return
self.helper(root.left,res)
res.append(root.val)
self.helper(root.right,res)
网友评论