亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Python--實現(xiàn)二叉樹的遍歷操作

系統(tǒng) 1783 0

一、首先二叉樹的定義:

            
              class TreeNode:
  def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None
            
          

? ? ? ? 構建一棵二叉樹:

            
              class Node(object):
    def __init__(self, val):
        self.val = val
        self.lchild = None
        self.rchild = None


class Tree(object):

    def __init__(self):
        self.root = None
        self.myQueue = []

    def add(self, val):
        node = Node(val)

        if self.root == None:
            self.root = node
            self.myQueue.append(self.root)
        else:
            while True:
                point = self.myQueue[0]

                if point.lchild == None:
                    point.lchild = node
                    self.myQueue.append(point.lchild)
                    return
                elif point.rchild == None:
                    point.rchild = node
                    self.myQueue.append(point.rchild)
                    self.myQueue.pop(0)
                    return

            
          

?

二、前序遍歷

            
              遞歸實現(xiàn)

def preorder(root,res=[]):
  if not root:
    return 
  res.append(root.val)
  preorder(root.lchild,res)
  preorder(root.rchild,res)
  return res

迭代實現(xiàn)

def preorder(root):
  res = []
  if not root: 
    return []
  stack = [root]
  while stack:
    node = stack.pop()
    res.append(node.val)
    if node.rchild:
      stack.append(node.rchild)
    if node.lchild:
      stack.append(node,lchild)
  return res
            
          

? 三、中序遍歷

            
              遞歸實現(xiàn)

def inorder(root,res=[]):
  if not root:
    return 
  inorder(root.lchild,res)
  res.append(root.val)
  inorder(root.rchild,res)
  return res


迭代實現(xiàn)

def inorder(root):
  stack = []
  node = root
  res = []
  while stack or node:
    while node:
      stack.append(node)
      node = node.lchild
    node = stack.pop()
    res.append(node.val)
    node = node.rchild
  return res

            
          

? 四、后序遍歷

            
              遞歸實現(xiàn)

def laorder(root,res=[]):
  if not root:
    return 
  laorder(root.lchild,res)
  laorder(root.rchild,res)
  res.append(root.val)
  return res



迭代實現(xiàn)

def laorder(root):
  stack = [root]
  res = []
  while stack:
    node = stack.pop()
    if node.lchild:
      stack.append(node.left)
    if node.rchild:
      stack.append(node.right)
    res.append(node.val)
  return res[::-1]
            
          

五、層次遍歷?

            
              迭代

def levelorder(root):
  queue = [root]
  res = []
  while queue:
    node = queue.pop(0)
    if node.lchild: 
      queue.append(node.lchild)
    if node.rchild:
      queue.append(node.rchild)
    res.append(node.val)
  return res
            
          

?運行結果:

Python--實現(xiàn)二叉樹的遍歷操作_第1張圖片 Python--實現(xiàn)二叉樹的遍歷操作_第2張圖片

?

?參考博客:https://www.jb51.net/article/157422.htm、https://blog.csdn.net/Bone_ACE/article/details/46718683、https://blog.csdn.net/wzngzaixiaomantou/article/details/81294915


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一区二区国产一区二区a4yy | 精品国产中文字幕 | 免费欧美日韩 | 日本成人毛片 | 九九综合视频 | 中文字幕日本精品一区二区三区 | 欧洲亚洲精品 | 欧美色久 | 天天摸夜夜摸爽爽狠狠婷婷97 | 亚洲综合激情五月色播 | 免费一级网站 | 开心片色99xxxx | 久久久这里只有精品免费 | 99视频在线观看免费 | 全部毛片免费看 | 亚洲六月丁香色婷婷综合久久 | 亚洲人成依人成综合网 | 国产日韩欧美中文 | 久热中文字幕在线精品首页 | 亚洲性免费 | 无毒不卡在线播放 | 国产精品久久久久影院免费 | 26uuu另类欧美亚洲日本 | se94se亚洲欧美在线 | 中文字幕在线精品 | 国产精品亚洲一区在线播放 | 亚洲视频福利 | 久久这里精品青草免费 | 好看的中文字幕在线 | 久久99久久 | 日本在线色 | 亚洲视频一二三 | 久热网| 国产97在线 | 亚洲 | 久久精品一区二区三区四区 | 久热精品视频在线播放 | 高清人人天天夜夜曰狠狠狠狠 | 97久久国语露脸精品对白 | 一级寡妇乱色毛片全18 | 伊人情人综合 | 网络毛片 |