首页 > 代码库 > LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

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;               
    }
};
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

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