C++實(shí)現(xiàn)LeetCode(103.二叉樹(shù)的之字形層序遍歷)
[LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉樹(shù)的之字形層序遍歷
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
這道二叉樹(shù)的之字形層序遍歷是之前那道 Binary Tree Level Order Traversal 的變形,不同之處在于一行是從左到右遍歷,下一行是從右往左遍歷,交叉往返的之字形的層序遍歷。最簡(jiǎn)單直接的方法就是利用層序遍歷,并使用一個(gè)變量 cnt 來(lái)統(tǒng)計(jì)當(dāng)前的層數(shù)(從0開(kāi)始),將所有的奇數(shù)層的結(jié)點(diǎn)值進(jìn)行翻轉(zhuǎn)一下即可,參見(jiàn)代碼如下:
解法一:
class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { if (!root) return {}; vector<vector<int>> res; queue<TreeNode*> q{{root}}; int cnt = 0; 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); } if (cnt % 2 == 1) reverse(oneLevel.begin(), oneLevel.end()); res.push_back(oneLevel); ++cnt; } return res; } };
我們可以將上面的解法進(jìn)行優(yōu)化一下,翻轉(zhuǎn)數(shù)組雖然可行,但是比較耗時(shí),假如能夠直接計(jì)算出每個(gè)結(jié)點(diǎn)值在數(shù)組中的坐標(biāo),就可以直接進(jìn)行更新了。由于每層的結(jié)點(diǎn)數(shù)是知道的,就是隊(duì)列的元素個(gè)數(shù),所以可以直接初始化數(shù)組的大小。此時(shí)使用一個(gè)變量 leftToRight 來(lái)標(biāo)記順序,初始時(shí)是 true,當(dāng)此變量為 true 的時(shí)候,每次加入數(shù)組的位置就是i本身,若變量為 false 了,則加入到 size-1-i 位置上,這樣就直接相當(dāng)于翻轉(zhuǎn)了數(shù)組。每層遍歷完了之后,需要翻轉(zhuǎn) leftToRight 變量,同時(shí)不要忘了將 oneLevel 加入結(jié)果 res,參見(jiàn)代碼如下:
解法二:
class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { if (!root) return {}; vector<vector<int>> res; queue<TreeNode*> q{{root}}; bool leftToRight = true; while (!q.empty()) { int size = q.size(); vector<int> oneLevel(size); for (int i = 0; i < size; ++i) { TreeNode *t = q.front(); q.pop(); int idx = leftToRight ? i : (size - 1 - i); oneLevel[idx] = t->val; if (t->left) q.push(t->left); if (t->right) q.push(t->right); } leftToRight = !leftToRight; res.push_back(oneLevel); } return res; } };
我們也可以使用遞歸的方法來(lái)解,這里實(shí)際上用的是先序遍歷,遞歸函數(shù)需要一個(gè)變量 level 來(lái)記錄當(dāng)前的深度,由于 level 是從0開(kāi)始的,假如結(jié)果 res 的大小等于 level,就需要在結(jié)果 res 中新加一個(gè)空集,這樣可以保證 res[level] 不會(huì)越界。取出 res[level] 之后,判斷 levle 的奇偶,若其為偶數(shù),則將 node->val 加入 oneLevel 的末尾,若為奇數(shù),則加在 oneLevel 的開(kāi)頭。然后分別對(duì) node 的左右子結(jié)點(diǎn)調(diào)用遞歸函數(shù),此時(shí)要傳入 level+1 即可,參見(jiàn)代碼如下:
解法三:
class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector<vector<int>> res; helper(root, 0, res); return res; } void helper(TreeNode* node, int level, vector<vector<int>>& res) { if (!node) return; if (res.size() <= level) { res.push_back({}); } vector<int> &oneLevel = res[level]; if (level % 2 == 0) oneLevel.push_back(node->val); else oneLevel.insert(oneLevel.begin(), node->val); helper(node->left, level + 1, res); helper(node->right, level + 1, res); } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/103
類似題目:
Binary Tree Level Order Traversal
參考資料:
https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(103.二叉樹(shù)的之字形層序遍歷)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)二叉樹(shù)的之字形層序遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹(shù))
- C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹(shù))
- C++實(shí)現(xiàn)LeetCode(104.二叉樹(shù)的最大深度)
- C++實(shí)現(xiàn)LeetCode(101.判斷對(duì)稱樹(shù))
- C++實(shí)現(xiàn)LeetCode(107.二叉樹(shù)層序遍歷之二)
- C++實(shí)現(xiàn)LeetCode(102.二叉樹(shù)層序遍歷)
- C++實(shí)現(xiàn)LeetCode(100.判斷相同樹(shù))
- C++實(shí)現(xiàn)LeetCode(110.平衡二叉樹(shù))
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(189.旋轉(zhuǎn)數(shù)組)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(189.旋轉(zhuǎn)數(shù)組),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07深入講解C++數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)函數(shù)的知識(shí)
這篇文章主要介紹了深入講解C++數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)函數(shù)的知識(shí),包括類型轉(zhuǎn)換運(yùn)算符函數(shù)等內(nèi)容,需要的朋友可以參考下2015-09-09C++實(shí)現(xiàn)Go的defer功能(示例代碼)
defer和go一樣都是Go語(yǔ)言提供的關(guān)鍵字。defer用于資源的釋放,會(huì)在函數(shù)返回之前進(jìn)行調(diào)用。接下來(lái)通過(guò)本文給大家介紹C++實(shí)現(xiàn)Go的defer功能,感興趣的朋友跟隨小編一起看看吧2021-07-07C/C++?QT實(shí)現(xiàn)解析JSON文件的示例代碼
JSON是一種輕量級(jí)的數(shù)據(jù)交換格式,它是基于ECMAScript的一個(gè)子集,使用完全獨(dú)立于編程語(yǔ)言的文本格式來(lái)存儲(chǔ)和表示數(shù)據(jù)。這篇文章主要介紹了QT實(shí)現(xiàn)解析JSON文件的示例代碼,需要的可以參考一下2022-01-01