4

JZ-022-从上往下打印二叉树

 2 years ago
source link: https://segmentfault.com/a/1190000041101723
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JZ-022-从上往下打印二叉树

发布于 31 分钟前

从上往下打印二叉树

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

题目链接: 从上往下打印二叉树

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) {

    }
}

【每日寄语】 每天给自己一个希望,试着不为明天而烦恼,不为昨天而叹息,只为今天更美好。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK