最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

N叉樹(shù)的三種遍歷(層次遍歷、前序遍歷、后序遍歷)

 更新時(shí)間:2022年04月14日 16:00:46   作者:BlackMan_阿偉  
本文主要介紹了N叉樹(shù)的三種遍歷(層次遍歷、前序遍歷、后序遍歷),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

題目鏈接:

590.N叉樹(shù)的后序遍歷

429.N叉樹(shù)的層序遍歷

598.N叉樹(shù)的前序遍歷

1、層次遍歷

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
 
class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        if not root:
            return []
 
        queue = collections.deque()
        queue.append(root)
        res = []
 
        while queue:
            size = len(queue)
            temp = []
            for _ in range(size):
                node = queue.popleft()
                temp.append(node.val)
                if node.children:
                    queue.extend(node.children)
            res.append(temp)
        
        return res

2、前序遍歷

前序遍歷就是從左至右,先根后孩子;遞歸比較簡(jiǎn)單,迭代法的話需要借助一個(gè)輔助棧,把每個(gè)節(jié)點(diǎn)的孩子都?jí)喝霔V校?/strong>

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
 
class Solution:
    def preorder(self, root: 'Node') -> List[int]:
        if not root:
            return []
        
        #迭代法
        stack, output = [root, ], []            
        while stack:
            root = stack.pop()
            output.append(root.val)
            stack.extend(root.children[::-1])
                
        return output
 
        #遞歸法
        res = []
 
        def helper(root):
            if not root:
                return 
            res.append(root.val)
            for children in root.children:
                helper(children)
        
        helper(root)
 
        return res

3、后序遍歷

在后序遍歷中,我們會(huì)先遍歷一個(gè)節(jié)點(diǎn)的所有子節(jié)點(diǎn),再遍歷這個(gè)節(jié)點(diǎn)本身。例如當(dāng)前的節(jié)點(diǎn)為 u,它的子節(jié)點(diǎn)為 v1, v2, v3 時(shí),那么后序遍歷的結(jié)果為 [children of v1], v1, [children of v2], v2, [children of v3], v3, u,其中 [children of vk] 表示以 vk 為根節(jié)點(diǎn)的子樹(shù)的后序遍歷結(jié)果(不包括 vk 本身)。我們將這個(gè)結(jié)果反轉(zhuǎn),可以得到 u, v3, [children of v3]', v2, [children of v2]', v1, [children of v1]',其中 [a]' 表示 [a] 的反轉(zhuǎn)。此時(shí)我們發(fā)現(xiàn),結(jié)果和前序遍歷非常類似,只不過(guò)前序遍歷中對(duì)子節(jié)點(diǎn)的遍歷順序是 v1, v2, v3,而這里是 v3, v2, v1。

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""
 
class Solution:
    def postorder(self, root: 'Node') -> List[int]:
        if not root:
            return []
 
        #后續(xù)遍歷是先遍歷一個(gè)節(jié)點(diǎn)的孩子節(jié)點(diǎn),在去遍歷這個(gè)節(jié)點(diǎn)本身
        
        #遞歸
        result = []
        def postHelper(root):
            if not root:
                return None
            children = root.children
            for child in children:
                postHelper(child)
            result.append(root.val)
 
        postHelper(root)
        return result
 
 
 
        #迭代法:輔助棧
        res = []
        stack = [root,]
 
        while stack:
            
            node = stack.pop()
            if node is not None:
                res.append(node.val)
            for children in node.children:
                stack.append(children)
        
        return res[::-1]

總結(jié):N叉樹(shù)和二叉樹(shù)的差別不是很多,唯一的差別就是孩子很多不需要去判斷左右孩子了。

到此這篇關(guān)于N叉樹(shù)的三種遍歷(層次遍歷、前序遍歷、后序遍歷)的文章就介紹到這了,更多相關(guān)N叉樹(shù)的三種遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

炉霍县| 连州市| 白山市| 大渡口区| 尚义县| 无棣县| 八宿县| 邹城市| 临桂县| 锡林浩特市| 抚松县| 台山市| 宁夏| 德格县| 鞍山市| 区。| 彭州市| 凯里市| 汶上县| 武平县| 葫芦岛市| 阿鲁科尔沁旗| 隆尧县| 普安县| 青州市| 湟中县| 鄂伦春自治旗| 深圳市| 怀化市| 高要市| 醴陵市| 伊吾县| 林周县| 西林县| 黑水县| 阳东县| 文化| 邵阳市| 南江县| 萍乡市| 新邵县|