资讯专栏INFORMATION COLUMN

【Leetcode】102. 二叉树的层次遍历

feng409 / 509人阅读

摘要:题目给定一个二叉树,返回其按层次遍历的节点值。例如给定二叉树返回其层次遍历结果题解我们数据结构的书上教的层序遍历就是利用一个队列不断的把左子树和右子树入队。

题目

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]
题解

我们数据结构的书上教的层序遍历,就是利用一个队列,不断的把左子树和右子树入队。但是这个题目还要要求按照层输出。所以关键的问题是: 如何确定是在同一层的
我们很自然的想到:
如果在入队之前,把上一层所有的节点出队,那么出队的这些节点就是上一层的列表。
由于队列是先进先出的数据结构,所以这个列表是从左到右的。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List> levelOrder(TreeNode root) {
        List> res = new LinkedList<>();
        if (root == null) {
            return res;
        }

        LinkedList queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List currentRes = new LinkedList<>();
            // 当前队列的大小就是上一层的节点个数, 依次出队
            while (size > 0) {
                TreeNode current = queue.poll();
                if (current == null) {
                    continue;
                }
                currentRes.add(current.val);
                // 左子树和右子树入队.
                if (current.left != null) {
                    queue.add(current.left);
                }
                if (current.right != null) {
                    queue.add(current.right);
                }
                size--;
            }
            res.add(currentRes);
        }
        return res;
    }
}

这道题可不可以用非递归来解呢?

递归的子问题:遍历当前节点, 对于当前层, 遍历左子树的下一层层,遍历右子树的下一层

递归结束条件: 当前层,当前子树节点是null

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List> levelOrder(TreeNode root) {
        List> res = new LinkedList<>();
        if (root == null) {
            return res;
        }
        levelOrderHelper(res, root, 0);
        return res;
    }

    /**
     * @param depth 二叉树的深度
     */
    private void levelOrderHelper(List> res, TreeNode root, int depth) {
        if (root == null) {
            return;
        }
        
        if (res.size() <= depth) {
            // 当前层的第一个节点,需要new 一个list来存当前层.
            res.add(new LinkedList<>());
        }
        // depth 层,把当前节点加入
        res.get(depth).add(root.val);
        // 递归的遍历下一层.
        levelOrderHelper(res, root.left, depth + 1);
        levelOrderHelper(res, root.right, depth + 1);
    }
}
热门阅读

技术文章汇总

【Leetcode】101. 对称二叉树

【Leetcode】100. 相同的树

【Leetcode】98. 验证二叉搜索树

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73439.html

相关文章

  • leetcode-102-Binary Tree Level Order Traversal

    102. 二叉树的层次遍历 题目描述 给定一个二叉树,返回其按层次遍历的节点值。 (即zhucengde,从左到右访问)。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回其层次遍历结果为: [ [3], [9,20], [15,7] ] class Solution: def le...

    widuu 评论0 收藏0
  • Leetcode】103. 二叉树的锯齿形层次遍历

    摘要:题目给定一个二叉树,返回其节点值的锯齿形层次遍历。例如给定二叉树返回锯齿形层次遍历如下题解这道题要求用字型就是要求知道深度。稍作改动的是需要在遍历左子树和右子树的时候带上深度的信息,才能知道是加在列表头还是列表尾部。 题目 给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。 例如:给定二叉树 [3,9,20,null...

    XiNGRZ 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 之 JavaScript 解答第94题 —— 二叉树的中序遍历

    摘要:小鹿题目二叉树中序遍历给定一个二叉树,返回它的中序遍历。通常递归的方法解决二叉树的遍历最方便不过,但是我还是喜欢增加点难度,用一般的迭代循环来实现。 Time:2019/4/25Title:Binary Tree Inorder TraversalDifficulty: MediumAuthor:小鹿 题目:Binary Tree Inorder Traversal(二叉树中序遍历...

    Jason 评论0 收藏0

发表评论

0条评论

feng409

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<