LeetCode 102 Binary Tree Level Order Traversal

题目


Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
层遍历,从左到右

思路


1  基本的基本,广度遍历,要写的熟练。

2 容易出错的地方 a queue不要写成stack b queue的函数是add 和remove  c 计数器何时使用 d 每次添加要new ArrayList 和clear一下


代码


public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        ArrayList<Integer> temp = new ArrayList<Integer>();
        if(root==null){
            return ans;
        }
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        int num =0;
        int count =1;
        while(!queue.isEmpty()){
            TreeNode cur = queue.remove();
            temp.add(cur.val);
            count--;
            if(cur.left!=null){
                queue.add(cur.left);
                num++;
            }
            if(cur.right!=null){
                queue.add(cur.right);
                num++;
            }
            if(count==0){
                ans.add(new ArrayList<Integer>(temp));
                temp.clear();
                count = num;
                num =0;
            }
        }
        return ans;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我无法理解你的问题。请供更多的上下或具体的问题描述,我将竭尽全力为您提供帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [分类整理leetcode算法题解,代码语言采用c++与python实现.zip](https://download.csdn.net/download/qq_35831906/88245562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [两两认识leetcode-ACM-ICPC-Preparation-master-ct:ACM-ICPC-Preparation-master](https://download.csdn.net/download/weixin_38656064/19943218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [【Leetcode】ACM模式的各种输入处理](https://blog.csdn.net/fisherish/article/details/120402338)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值