The binary print into multiple lines.
Title Description
top to bottom, binary print layer, the same layer as the output node from left to right. Each line of output layer.
Problem-solving ideas
this problem with the addition of a question very similar, very similar problem-solving ideas, refer to the answer can be seen here .
Reference Code
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回二维列表[[1,2],[4,5]]
def Print(self, pRoot):
# write code here
result = []
if not pRoot:
return result
currnodes = [pRoot]
while currnodes:
currvalues = []
nextnodes = []
for node in currnodes:
currvalues.append(node.val)
if node.left:
nextnodes.append(node.left)
if node.right:
nextnodes.append(node.right)
result.append(currvalues[:])
currnodes = nextnodes
return result