Sword finger Offer: print binary tree into multi line Java/Python

1. Title Description

The binary tree is printed from top to bottom, and the nodes of the same layer are output from left to right. Output one line per layer.

2. Algorithm description

It is the sequence traversal of binary tree, which can be achieved by applying the sequence traversal template. It is the sequence traversal of binary tree, which can be achieved by applying the sequence traversal template. It is the sequence traversal of binary tree, which can be achieved by applying the sequence traversal template.
Just notice here that each layer is placed in a list, and the result is the list. Just notice here that each layer is placed in a list, and the result is the list. Just notice here that each layer is placed in a list, and the result is the list.
When traversing each layer, first record the number of elements in the current queue (the number of elements in the current queue is the number of elements in a certain layer). See the code for details. \red {first record the number of elements in the current queue (the number of elements in the current queue is the number of elements in a certain layer). See the code for details. }First record the number of elements in the current queue (the number of elements in the current queue is the number of elements in a certain layer). See the code for details.

3. Code description

3.1.Java code

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
import java.util.*;
public class Solution {
    ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
        if(pRoot == null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        ArrayList<Integer> temp = null;
        queue.offer(pRoot);
        while(!queue.isEmpty()){
            int cnt = 0, len = queue.size();
            temp = new ArrayList<>();
            while(cnt++ < len){
                TreeNode node = queue.poll();
                temp.add(node.val);
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
            ans.add(temp);
        }
        return ans;
    }
    
}

3.2.Python code

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # Return to 2D list [[1,2], [4,5]]
    def Print(self, pRoot):
        ans = []
        if not pRoot:
            return ans
        queue = []
        queue.append(pRoot)
        while queue:
            cnt = 0
            size = len(queue)
            row = []
            while cnt<size:
                node = queue.pop(0)
                row.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
                cnt += 1
            ans.append(row)
        return ans

Keywords: Java Python

Added by john_6767 on Fri, 08 Nov 2019 21:20:29 +0200