py3 把二叉树打印成多行
把二叉树打印成多行
http://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
思路:用两个列表分别保存当前层节点和下一层节点,结果.append([i.val for i in 当前层节点]),然后 当前层列表,下一层列表 = 下一层列表, [] 即可,直到当前层为空
class Solution: def Print(self, pRoot): # write code here if not pRoot: return [] current = [pRoot] next_layer = [] result = [] while current: for i in current: if i.left: next_layer.append(i.left) if i.right: next_layer.append(i.right) result.append([i.val for i in current]) current,next_layer = next_layer,[] return result