4
头图

Print binary tree from top to bottom

Title description

Print out each node of the binary tree from top to bottom, and print the nodes at the same level from left to right.

topic link : from top to bottom

Code

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

/**
 * 标题:从上往下打印二叉树
 * 题目描述
 * 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
 * 题目链接:
 * https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701?tpId=13&&tqId=11175&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz22 {

    /**
     * 使用队列来进行层次遍历。
     * <p>
     * 不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。
     *
     * @param root
     * @return
     */
    public ArrayList<Integer> printFromTopToBottom(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        ArrayList<Integer> result = new ArrayList<Integer>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int cnt = queue.size();
            while (cnt-- > 0) {
                TreeNode t = queue.poll();
                if (t == null) {
                    continue;
                }
                result.add(t.val);
                queue.add(t.left);
                queue.add(t.right);
            }
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
【Daily Message】 Give yourself a hope every day, try not to worry about tomorrow, not to sigh for yesterday, just for today to be better.

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

玉树临风,仙姿佚貌!