while i < len(v): print(v[i], end=" ") i += 1 print() if root.lchild is not None: FindRoad(root.lchild, num, sums, v) if root.rchild is not None: FindRoad(root.rchild, num, sums, v) # 清除遍历的路径 sums -= v[-1] v.remove(v[-1]) if __name__ == '__main__': arr = [ i for i in range(1, 11) ] root = Arr2Tree(arr, 0, len(arr)-1) s = [] FindRoad(root, 12, 0, s) print(s)
from Array import RandArr def IsEqual(root1, root2: BiTNode): if (root1 and root2) is None: return True if root1 is None and root2 is not None: return False if root1 is not None and root2 is None: return False if root1.data == root2.data: return IsEqual(root1.lchild, root2.lchild) and \ IsEqual(root1.rchild, root2.rchild) else: return False if __name__ == '__main__': length = 10 arr = RandArr(length) root1 = Arr2Tree(arr, 0, length - 1) root2 = Arr2Tree(arr, 0, length - 1) root3 = Arr2Tree(RandArr(length), 0, length - 1) print(IsEqual(root1, root2)) print(IsEqual(root1, root3))
while root.rchild is not None: root = root.rchild return root def GetNode(root: BiTNode): maxNode = GetMax(root) minNode = GetMin(root) # 找到中间值 mid = (maxNode.data + minNode.data) / 2 result = None while root is not None: if root.data <= mid: root = root.rchild else: result = root root = root.lchild return result if __name__ == '__main__': root = Arr2Tree(RandArr(10), 0, 9) Node = GetNode(root) print(Node.data)
root.lchild = link.pEnd if link.pEnd is None: link.pHead = root else: link.pEnd.rchild = root link.pEnd = root InOrderBSTree(root.rchild) if __name__ == '__main__': length = 10 arr = RandArr(length) root = Arr2Tree(arr, 0, length - 1) InOrderBSTree(root) cur = link.pHead while cur is not None: print(cur.data, end=" ") cur = cur.rchild print() cur = link.pEnd while cur is not None: print(cur.data, end=" ") cur = cur.lchild