C++實(shí)現(xiàn)LeetCode(156.二叉樹的上下顛倒)
[LeetCode] 156. Binary Tree Upside Down 二叉樹的上下顛倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.
Example:
Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5Output: return the root of the binary tree [4,5,2,#,#,3,1]
4
/ \
5 2
/ \
3 1
Clarification:
Confused what [4,5,2,#,#,3,1] means? Read more below on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as [1,2,3,#,#,4,#,#,5].
這道題讓我們把一棵二叉樹上下顛倒一下,而且限制了右節(jié)點(diǎn)要么為空要么一定會有對應(yīng)的左節(jié)點(diǎn)。上下顛倒后原來二叉樹的最左子節(jié)點(diǎn)變成了根節(jié)點(diǎn),其對應(yīng)的右節(jié)點(diǎn)變成了其左子節(jié)點(diǎn),其父節(jié)點(diǎn)變成了其右子節(jié)點(diǎn),相當(dāng)于順時(shí)針旋轉(zhuǎn)了一下。對于一般樹的題都會有迭代和遞歸兩種解法,這道題也不例外,先來看看遞歸的解法。對于一個(gè)根節(jié)點(diǎn)來說,目標(biāo)是將其左子節(jié)點(diǎn)變?yōu)楦?jié)點(diǎn),右子節(jié)點(diǎn)變?yōu)樽笞庸?jié)點(diǎn),原根節(jié)點(diǎn)變?yōu)橛易庸?jié)點(diǎn),首先判斷這個(gè)根節(jié)點(diǎn)是否存在,且其有沒有左子節(jié)點(diǎn),如果不滿足這兩個(gè)條件的話,直接返回即可,不需要翻轉(zhuǎn)操作。那么不停的對左子節(jié)點(diǎn)調(diào)用遞歸函數(shù),直到到達(dá)最左子節(jié)點(diǎn)開始翻轉(zhuǎn),翻轉(zhuǎn)好最左子節(jié)點(diǎn)后,開始回到上一個(gè)左子節(jié)點(diǎn)繼續(xù)翻轉(zhuǎn)即可,直至翻轉(zhuǎn)完整棵樹,參見代碼如下:
解法一:
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
if (!root || !root->left) return root;
TreeNode *l = root->left, *r = root->right;
TreeNode *res = upsideDownBinaryTree(l);
l->left = r;
l->right = root;
root->left = NULL;
root->right = NULL;
return res;
}
};
下面我們來看迭代的方法,和遞歸方法相反的時(shí),這個(gè)是從上往下開始翻轉(zhuǎn),直至翻轉(zhuǎn)到最左子節(jié)點(diǎn),參見代碼如下:
解法二:
class Solution {
public:
TreeNode *upsideDownBinaryTree(TreeNode *root) {
TreeNode *cur = root, *pre = NULL, *next = NULL, *tmp = NULL;
while (cur) {
next = cur->left;
cur->left = tmp;
tmp = cur->right;
cur->right = pre;
pre = cur;
cur = next;
}
return pre;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/156
類似題目:
參考資料:
https://leetcode.com/problems/binary-tree-upside-down/
https://leetcode.com/problems/binary-tree-upside-down/discuss/49412/Clean-Java-solution
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(156.二叉樹的上下顛倒)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)二叉樹的上下顛倒內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 二維數(shù)組參數(shù)傳遞的實(shí)現(xiàn)方法
這篇文章主要介紹了C++ 二維數(shù)組參數(shù)傳遞的實(shí)現(xiàn)方法的相關(guān)資料,這里提供三種方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
Visual Studio 2019 Professional 激活方法詳解
這篇文章主要介紹了Visual Studio 2019 Professional 激活方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
淺談#ifndef,#define,#endif的作用和用法
下面小編就為大家?guī)硪黄獪\談#ifndef,#define,#endif的作用和用法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
C語言數(shù)據(jù)結(jié)構(gòu)之單向鏈表詳解
單向鏈表(單鏈表)是鏈表的一種,其特點(diǎn)是鏈表的鏈接方向是單向的,對鏈表的訪問要通過順序讀取從頭部開始。本文將為大家詳細(xì)講講單向鏈表的實(shí)現(xiàn)與使用,需要的可以參考一下2022-08-08
C++11中初始化列表initializer lists的使用方法
C++11引入了初始化列表來初始化變量和對象,自定義類型,如果想用初始化列表就要包含initializer_list頭文件2021-09-09

