题目链接
https://leetcode.com/problems/binary-tree-level-order-traversal/
题目原文
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
return its level order traversal as:
题目大意
层次遍历一棵二叉树
解题思路
使用dfs实现,并且在遍历中设立一个level值记录节点高度
代码
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def help(self, root, level, ans):
if root:
if len(ans) < level + 1:
ans.append([])
ans[level].append(root.val)
self.help(root.left, level + 1, ans)
self.help(root.right, level + 1, ans)
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
ans = []
self.help(root, 0, ans)
return ans