Created
December 20, 2014 00:32
-
-
Save itissid/e19a2d2c804b864599f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution: | |
| # @param root, a tree node | |
| # @return a list of lists of integers | |
| def levelOrder(self, root): | |
| self.level_order_list = [] | |
| self.dfs_traverse(root, l = 0) | |
| return self.level_order_list | |
| def dfs_traverse(self, root, l): | |
| if not root: | |
| return | |
| if len(self.level_order_list) == l or l == 0: | |
| # List does not have the level | |
| self.level_order_list.append([root.val]) | |
| elif len(self.level_order_list) >= l + 1: | |
| # List has the level | |
| self.level_order_list[l].append(root.val) | |
| self.dfs_traverse(root.left, l + 1) | |
| self.dfs_traverse(root.right, l + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment