_ To prove safety offer_ binary tree binary tree print into multiple lines.

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 
Published 31 original articles · won praise 0 · Views 712

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105180596