欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(107.二叉樹(shù)層序遍歷之二)

 更新時(shí)間:2021年07月21日 14:57:10   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(107.二叉樹(shù)層序遍歷之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[LeetCode] 107. Binary Tree Level Order Traversal II 二叉樹(shù)層序遍歷之二

Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: [[15,7],[9,20],[3]]

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • -1000 <= Node.val <= 1000

從底部層序遍歷其實(shí)還是從頂部開(kāi)始遍歷,只不過(guò)最后存儲(chǔ)的方式有所改變,可以參見(jiàn)博主之前的博文 Binary Tree Level Order Traversal, 參見(jiàn)代碼如下:

解法一:

class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode* root) {
        if (!root) return {};
        vector<vector<int>> res;
        queue<TreeNode*> q{{root}};
        while (!q.empty()) {
            vector<int> oneLevel;
            for (int i = q.size(); i > 0; --i) {
                TreeNode *t = q.front(); q.pop();
                oneLevel.push_back(t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
            res.insert(res.begin(), oneLevel);
        }
        return res;
    }
};

下面來(lái)看遞歸的解法,由于遞歸的特性,我們會(huì)一直深度優(yōu)先去處理左子結(jié)點(diǎn),那么勢(shì)必會(huì)穿越不同的層,所以當(dāng)要加入某個(gè)結(jié)點(diǎn)的時(shí)候,必須要知道當(dāng)前的深度,所以使用一個(gè)變量 level 來(lái)標(biāo)記當(dāng)前的深度,初始化帶入0,表示根結(jié)點(diǎn)所在的深度。由于需要返回的是一個(gè)二維數(shù)組 res,開(kāi)始時(shí)由于不知道二叉樹(shù)的深度,不知道有多少層,所以無(wú)法實(shí)現(xiàn)申請(qǐng)好二維數(shù)組的大小,只有在遍歷的過(guò)程中不斷的增加。那么什么時(shí)候該申請(qǐng)新的一層了呢,當(dāng) level 等于二維數(shù)組的大小的時(shí)候,為啥是等于呢,不是說(shuō)要超過(guò)當(dāng)前的深度么,這是因?yàn)?level 是從0開(kāi)始的,就好比一個(gè)長(zhǎng)度為n的數(shù)組A,你訪(fǎng)問(wèn) A[n] 是會(huì)出錯(cuò)的,當(dāng) level 等于數(shù)組的長(zhǎng)度時(shí),就已經(jīng)需要新申請(qǐng)一層了,新建一個(gè)空層,繼續(xù)往里面加數(shù)字,參見(jiàn)代碼如下:

解法二:

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        levelorder(root, 0, res);
        return vector<vector<int>> (res.rbegin(), res.rend());
    }
    void levelorder(TreeNode* node, int level, vector<vector<int>>& res) {
        if (!node) return;
        if (res.size() == level) res.push_back({});
        res[level].push_back(node->val);
        if (node->left) levelorder(node->left, level + 1, res);
        if (node->right) levelorder(node->right, level + 1, res);
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/107

類(lèi)似題目:

Average of Levels in Binary Tree

Binary Tree Zigzag Level Order Traversal

Binary Tree Level Order Traversal

類(lèi)似題目:

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/35089/Java-Solution.-Using-Queue

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/34981/My-DFS-and-BFS-java-solution

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(107.二叉樹(shù)層序遍歷之二)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)二叉樹(shù)層序遍歷之二內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論