日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統(tǒng) 1719 0

一、首先二叉樹的定義:

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

? ? ? ? 構(gòu)建一棵二叉樹:

            
              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

            
          

?

二、前序遍歷

            
              遞歸實(shí)現(xiàn)

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

迭代實(shí)現(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
            
          

? 三、中序遍歷

            
              遞歸實(shí)現(xiàn)

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


迭代實(shí)現(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

            
          

? 四、后序遍歷

            
              遞歸實(shí)現(xiàn)

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



迭代實(shí)現(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
            
          

?運(yùn)行結(jié)果:

Python--實(shí)現(xiàn)二叉樹的遍歷操作_第1張圖片 Python--實(shí)現(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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 长白| 安国市| 佛教| 邓州市| 鄄城县| 富裕县| 珠海市| 开化县| 泉州市| 松滋市| 潮安县| 师宗县| 山西省| 宜宾市| 广安市| 湟中县| 和田市| 东阿县| 永寿县| 吴桥县| 邹平县| 彭山县| 太原市| 建平县| 大荔县| 潜山县| 荔浦县| 河西区| 平江县| 绵竹市| 拜泉县| 永康市| 枝江市| 崇礼县| 行唐县| 东港市| 新丰县| 临湘市| 濮阳县| 耒阳市| 雷波县|