【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】

【102-Binary Tree Level Order Traversal(二叉树层序遍历)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  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]
]

题目大意

  给定一个二叉树,输出它的每一层的结点。

解题思路

  使用两队列,一个保存当前处理的层,一个保存下一次要处理的层。只到每一层都处理完。

代码实现

树结点类

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

算法实现类

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {

        List<List<Integer>> result = new ArrayList<>();

        if (root == null) {
            return result;
        }

        Deque<TreeNode> cur = new LinkedList<>();
        Deque<TreeNode> sub = new LinkedList<>();
        Deque<TreeNode> exc;

        TreeNode node;
        cur.addLast(root);

        while (!cur.isEmpty()) {
            List<Integer> layout = new LinkedList<>();
            while (!cur.isEmpty()) {
                node = cur.removeFirst();
                layout.add(node.val);

                if (node.left != null) {
                    sub.addLast(node.left);
                }

                if(node.right != null) {
                    sub.addLast(node.right);
                }
            }

            exc = cur;
            cur = sub;
            sub = exc;

            result.add(layout);
        }

        return result;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47354349

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-28 23:20:55

【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】的相关文章

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] ] 层序遍历二叉

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

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,null,null,15,7],     3    /   9  20     /     15   7 re

【LeetCode】Binary Tree Level Order Traversal(层序遍历)

Question 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

Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List&lt;list&gt;

问题描述: 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15

Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

二叉树的层次遍历 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<in

102 Binary Tree Level Order Traversal 二叉树的层次遍历

给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其层次遍历结果为:[  [3],  [9,20],  [15,7]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal/description/ /** * Definition for

leetcode 题解: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] ] co

刷题102. Binary Tree Level Order Traversal

一.题目说明 题目102. Binary Tree Level Order Traversal,给一个二叉树,返回按层遍历的各节点,每层返回一个数组. 二.我的解答 这个题目是普通的二叉树按层遍历,没什么难度.代码如下: class Solution{ public: vector<vector<int>> levelOrder(TreeNode* root){ vector<vector<int>> r; TreeNode* p; if(root ==NU

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

leetcode—102 Binary Tree Level Order Traversal(二叉树的层次遍历)

Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478 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},