4
头图

print binary tree on multiple lines

Topic description

Print the binary tree layer by layer from top to bottom, and the nodes in the same layer are output from left to right. Each layer outputs one line.

topic link : the binary tree into multiple lines

code

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 标题:把二叉树打印成多行
 * 题目描述
 * 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
 * 题目链接:
 * https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288?tpId=13&&tqId=11213&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz60 {

    /**
    * 递归
    **/
    ArrayList<ArrayList<Integer>> print(TreeNode pRoot) {
        Queue<TreeNode> treeNodes = new LinkedList<TreeNode>();
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        treeNodes.add(pRoot);
        while (!treeNodes.isEmpty()) {
            Queue<TreeNode> curNodes = new LinkedList<TreeNode>();
            curNodes.addAll(treeNodes);
            while (!treeNodes.isEmpty()) {
                treeNodes.poll();
            }
            ArrayList<Integer> curList = new ArrayList<Integer>();
            while (!curNodes.isEmpty()) {
                TreeNode t = curNodes.poll();
                if (t == null) {
                    continue;
                }
                curList.add(t.val);
                treeNodes.add(t.left);
                treeNodes.add(t.right);
            }
            if (!curList.isEmpty()) {
                result.add(curList);
            }
        }
        return result;
    }

    public static void main(String[] args) {
    
    }
}
[Daily Message] Please try harder quietly, I hope one day you can say that sentence, and I finally become a person who lives up to expectations.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!