人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動(dòng)態(tài)

python二叉樹(shù)常用算法總結(jié)

發(fā)布日期:2021-12-30 22:53 | 文章來(lái)源:源碼中國(guó)

1.1 二叉樹(shù)的初始化

#initial of BinaryTree
class BinaryTree:
 def __init__(self,rootObj):
  self.val = rootObj
  self.left = None
  self.right = None
 def insertLeft(self,newNode):
  if self.left == None:
self.left = BinaryTree(newNode)
  else:
t = BinaryTree(newNode)
t.left = self.left
self.left = t
 def insertRight(self,newNode):
  if self.right == None:
self.right = BinaryTree(newNode)
  else:
t = BinaryTree(newNode)
t.right = self.right
self.right = t

1.2 創(chuàng)建一個(gè)二叉樹(shù)

#create a BinaryTree [18,7,11,3,4,5,6,#,#,#,#,1,3,2,4]
#  18
# 7  11
#3 4 5 6
#1 3 2 4
root = BinaryTree(18)
root.left = BinaryTree(7)
root.right = BinaryTree(11)
root.left.left = BinaryTree(3)
root.left.right = BinaryTree(4)
root.right.left = BinaryTree(5)
root.right.right = BinaryTree(6)
root.right.left.left = BinaryTree(1)
root.right.left.right = BinaryTree(3)
root.right.right.left = BinaryTree(2)
root.right.right.right = BinaryTree(4)

1.3 前序遍歷

#遞歸版本
def PreOrder(self, node):
 if node:
  print(node.val)
  self.PreOrder(node.left)
  self.PreOrder(node.right)
#循環(huán)版本
def PreOrderLoop(self, node):
 if node == None:
  return
 stack =[]
 print(node.val)
 stack.append(node)
 node = node.left
 while stack!=[] or node:
  while node:
print(node.val)
stack.append(node)
node = node.left
  node = stack[-1].right
  stack.pop()
#ouput: 18 7 3 4 11 5 1 3 6 2 4 

1.4 中序遍歷

#遞歸版本
def InOrder(self, node):
 if node:
  self.InOrder(node.left)
  print(node.val)
  self.InOrder(node.right)
#循環(huán)版本
def InOrderLoop(self, node):
 if node == None:
  return None
 stack = []
 stack.append(node)
 node = node.left
 while stack!=[] or node:
  while node:
stack.append(node)
node = node.left
  print(stack[-1].val)
  node = stack[-1].right
  stack.pop()
#output:3 7 4 18 1 5 3 11 2 6 4

1.5 后序遍歷

#遞歸
def PostOrder(self, node):
 if node:
  self.PostOrder(node.left)
  self.PostOrder(node.right)
  print(node.val)
#非遞歸
def PostOrderLoop(self, node):
 if node == None:
  return
 stack =[]
 stack.append(node)
 pre = None
 while stack!=[]:
  node = stack[-1]
  if ((node.left==None and node.right==None) or
 (pre and (pre == node.left or pre ==node.right))):
print(node.val)
pre = node
stack.pop()
  else:
if node.right:
 stack.append(node.right)
if node.left:
 stack.append(node.left)
#output:3 4 7 1 3 5 2 4 6 11 18

1.6 層序遍歷

def LevelOrder(self, node):
 if node == None:
  return
 stack = []
 stack.append(node)
 while stack!=[]:
  node = stack[0]
  if node.left:
stack.append(node.left)
  if node.right:
stack.append(node.right)
  print(node.val)
  stack.pop(0)
output: 18 7 11 3 4 5 6 1 3 2 4

1.7 計(jì)算節(jié)點(diǎn)數(shù)

#遞歸版本
def CountNode(self, root):
 if root == None:
  return 0
 return self.CountNode(root.left) + self.CountNode(root.right) + 1
#非遞歸版本
def CountNodeNotRev(self, root):
 if root == None:
  return 0
 stack = []
 stack.append(root)
 index = 0
 while index<len(stack):
  if stack[index].left:
stack.append(stack[index].left)
  if stack[index].right:
stack.append(stack[index].right)
  index += 1
 print(len(stack))
output: 11

1.8 計(jì)算樹(shù)的深度

def getTreeDepth(self, root):
 if root == None:
  return 0
 left = self.getTreeDepth(root.left) + 1
 right = self.getTreeDepth(root.right) + 1
 return left if left>right else right

1.9 計(jì)算樹(shù)的葉子樹(shù)

def countLeaves(self, root):
 if root == None:
  return 0
 if root.left==None and root.right==None:
  return 1
 return self.countLeaves(root.left)+self.countLeaves(root.right)

1.10 獲取第K層節(jié)點(diǎn)數(shù)

def getKLevel(self, root, K):
 if root == None: return 0
 if K == 1: return 1
 return self.getKLevel(root.left, K-1)+self.getKLevel(root.right, K-1)

1.11 判斷兩顆二叉樹(shù)是否相同

def StrucCmp(self, root1, root2):
 if root1 == None and root2 == None: return True
 elif root1 ==None or root2 == None: return False
 return self.StrucCmp(root1.left, root2.left) and self.StrucCmp(root1.right, root2.right)

1.12 二叉樹(shù)的鏡像

def Mirror(self, root):
 if root == None: return
 tmp = root.left
 root.left = root.right
 root.right = tmp
 self.Mirror(root.left)
 self.Mirror(root.right)

1.13 找最低公共祖先節(jié)點(diǎn)

def findLCA(self, root, node1, node2):
 if root == None: return
 if root == node1 or root == node2: return root
 left = self.findLCA(root.left, node1, node2)
 right = self.findLCA(root.right, node1, node2)
 if left and right:
  return root
 return left if left else right

1.14 獲取兩個(gè)節(jié)點(diǎn)的距離

def getDist(self, root, node1, node2):
 lca = self.findLCA(root, node1, node2) #找最低公共祖宗節(jié)點(diǎn)
 level1 = self.FindLevel(lca, node1) #祖節(jié)點(diǎn)到兩個(gè)節(jié)點(diǎn)的距離
 level2 = self.FindLevel(lca, node2)
 return level1+level2
def FindLevel(self, node, target):
 if node == None: return -1
 if node == target: return 0
 level = self.FindLevel(node.left, target)
 if level == -1: level = self.FindLevel(node.right, target)
 if level != -1: return level + 1
 return -1

1.15 找一個(gè)節(jié)點(diǎn)的所有祖宗節(jié)點(diǎn)

def findAllAncestor(self, root, target):
 if root == None: return False
 if root == target: return True
 if self.findAllAncestor(root.left, target) or self.findAllAncestor(root.right, target):
  print(root.val)
  return True
 return False

到此這篇關(guān)于python二叉樹(shù)常用算法總結(jié)的文章就介紹到這了,更多相關(guān)python二叉樹(shù)常用算法,內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部