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

分析

这道题和上一题是一毛一样的,我就直接复制了。

首先定义了用于最后返回的vecAns,而后判断root是否为空,是的话直接返回不做添加操作。构造的queue中int用于存放层级信息,TreeNode*用于存放节点。

vector<vector<int>> vecAns;
if (!root) return vecAns;
queue<pair<int, TreeNode*>> queueTree;

接下来定义了map,它的优势在于可以随时指定键来添加值,这里就是指定层级来添加信息,后面的是vector就是用于存放树节点的。root的层级设定为0,后面用make_pair来构造pair对,最后添加到queue中。

map<int, vector<int>> mapAns;
int rootLevel = 0,  currentLevel =0;
queueTree.push(make_pair(rootLevel, root));

只要queue不为空就一直循环。每次一开始就解析出当前队列顶部的层级信息以及当前节点,将它添加到map中。添加完之后就可以弹出了。继续判断左右子树,如果为空就先添加到queue中等待下一部操作。待到下一次循环时,就是将它们添加到map中了。

while (!queueTree.empty()) {
    currentLevel = (queueTree.front().first);
    TreeNode *currentNode = (queueTree.front().second);
    mapAns[currentLevel].push_back(currentNode->val);
    queueTree.pop();
    if (currentNode->left != NULL)
        queueTree.push(make_pair(currentLevel + 1, currentNode->left));
    if (currentNode->right != NULL)
        queueTree.push(make_pair(currentLevel + 1, currentNode->right));
}            

将map中的信息逐个push到vector里,最后就直接return了。

for (auto iter = mapAns.rbegin(); iter != mapAns.rend(); ++iter) {
    vecAns.push_back(iter->second);
}
return vecAns;

这是上一题的解法,现在的输出顺序和之前是相反的。既然是map,我们完全可以根据层级信息的随意输出了。

for (int i = 0; i <= currentLevel; ++i) {
    vecAns.push_back(mapAns[i]);
}

Ok,大家可以去看看上一题:

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

代码

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vecAns;
        if (!root) return vecAns;
        queue<pair<int, TreeNode*>> queueTree;
        map<int, vector<int>> mapAns;
        int rootLevel = 0, currentLevel = 0;
        queueTree.push(make_pair(rootLevel, root));
        while (!queueTree.empty()) {
            currentLevel = (queueTree.front().first);
            TreeNode *currentNode = (queueTree.front().second);
            mapAns[currentLevel].push_back(currentNode->val);
            queueTree.pop();
            if (currentNode->left != NULL)
                queueTree.push(make_pair(currentLevel + 1, currentNode->left));
            if (currentNode->right != NULL)
                queueTree.push(make_pair(currentLevel + 1, currentNode->right));
        }
        for (int i = 0; i <= currentLevel; ++i) {
            vecAns.push_back(mapAns[i]);
        }
        return vecAns;
    }
};
时间: 2024-12-16 16:22:14

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

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

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

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

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

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

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

[LeetCode] 102. Binary Tree Level Order Traversal 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7

Java 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] ] 解题思路:

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