Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]

题目:

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},

return its level order traversal as:

[

[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; }
 * }
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        List<Integer> one=new ArrayList<>();
        one.add(root.val);
        result.add(one);
        List<List<Integer>> left=levelOrder(root.left);
        List<List<Integer>> right=levelOrder(root.right);
        for(int i=0;i<left.size()||i<right.size();i++){
            List<Integer> item=new ArrayList<>();
            if(i<left.size()){
                for(Integer k:left.get(i)){
                    item.add(k);
                }
            }
            if(i<right.size()){
                for(Integer k:right.get(i)){
                    item.add(k);
                }
            }
            result.add(item);
        }
        return result;
    }
}
时间: 2024-11-05 10:24:47

Leet Code OJ 102. Binary Tree Level Order Traversal [Difficulty: Easy]的相关文章

Leet Code OJ 107. Binary Tree Level Order Traversal II [Difficulty: Easy]

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, return its bottom-up level order traversal as: [ [

刷题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(二叉树的层次遍历)

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},

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

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

LeetCode OJ 107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

102. Binary Tree Level Order Traversal Java Solutions

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

102. Binary Tree Level Order Traversal (Tree, Queue; BFS)

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

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