python二叉树常用算法总结
程序员文章站
2022-07-05 09:48:49
目录1.10 获取第k层节点数1.1 二叉树的初始化#initial of binarytreeclass binarytree: def __init__(self,rootobj):...
1.1 二叉树的初始化
#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 创建一个二叉树
#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) #循环版本 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) #循环版本 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 计算节点数
#递归版本 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 计算树的深度
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 计算树的叶子树
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层节点数
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 判断两颗二叉树是否相同
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 二叉树的镜像
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 找最低公共祖先节点
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 获取两个节点的距离
def getdist(self, root, node1, node2): lca = self.findlca(root, node1, node2) #找最低公共祖宗节点 level1 = self.findlevel(lca, node1) #祖节点到两个节点的距离 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 找一个节点的所有祖宗节点
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
到此这篇关于python
二叉树常用算法总结的文章就介绍到这了,更多相关python二叉树常用算法,内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 扎克伯格展示的并不是元宇宙的全部