习题33列表的遍历

该程序展示了如何使用Python的while循环来迭代并添加元素到列表中。初始化i为0,当i小于6时,循环会打印当前元素的索引(0-5),并将i添加到numbers列表中。每次循环后,更新i的值并显示当前列表的内容。最终列表为[0,1,2,3,4,5],并打印了i的最终值(6)。

i = 0#赋给i变量的初始值0
numbers = []#列表number是空值
while i < 6:#i是小于6的
    print("At the top i is %d"%i)#%d时整型数字站位符,打印输出列表元素的第几元素0,1,2,3,4,5,
    numbers.append(i)#给列表i增加元素
    i = i + 1#给列表的元素加一,变了加一到第6个元素5结束
    print("Numbers now:",numbers)#打印出变化的列表,第一个列表[0],第二个列表[0,1]第三个列表[0,1,2][0,1,2,3][0,1,2,3,4]
    print("At the bottom i is %d"%i)#%d时整型数字站位符,打印输出列表元素的编号第几元素1,2,3,4,5,6
#print("the numbers:")#字符串原样打印输出
#for num in numbers:#把number赋值给num
   # print(num)#打印输出num

打印输出列表元素的第几元素0,1,2,3,4,5

打印出变化的列表,第一个列表[0],第二个列表[0,1]第三个列表[0,1,2][0,1,2,3][0,1,2,3,4]

打印输出列表元素的编号第几元素1,2,3,4,5,6

D:\软件\py36\python.exe E:\python\day15\31\xiti33.py 
At the top i is 0#列表里第一个元素0
Numbers now: [0]#第一个列表
At the bottom i is 1#第一个编号1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6

进程已结束,退出代码0
### 关于二叉树遍历的练习题 以下是几道典型的二叉树遍历题目,涉及已知两种遍历结果求第三种遍历结果: #### 题目一 **描述**: 已知某棵二叉树的先序遍历为 `ABDEGHCFIJ`,中序遍历为 `DBGEHACIFJ`,试求其后序遍历。 **解答思路**: 1. 根据先序遍历的第一个节点确定根节点为 A[^2]。 2. 在中序遍历中定位到 A 的位置,左侧部分为左子树 (即 DBGEH),右侧部分为右子树 (即 CIFJ)[^3]。 3. 对左子树和右子树分别重复上述过程,逐步构建完整的二叉树结构。 4. 最终得到后序遍历结果为:`DHEGBFJICA`。 --- #### 题目二 **描述**: 某二叉树的中序遍历为 `DEFBCGA`,后序遍历为 `DFEBGCA`,试求其先序遍历。 **解答思路**: 1. 后序遍历最后一个节点必然是整棵树的根节点 C[^3]。 2. 定位 C 在中序遍历中的位置,分割出左子树 DEF 和右子树 BGA。 3. 进一步分析左右子树内部关系,最终得出先序遍历结果为:`CDBGFEA`。 --- #### 题目三 **描述**: 若一棵二叉树的先序遍历为 `KLMNOPQR`,后序遍历为 `LPMRQONK`,试求其中序遍历。 **解答思路**: 1. 先序遍历第一个节点 K 是整个二叉树的根节点[^1]。 2. 后序遍历最后一个是根节点 K,在此之前的部分属于左右子树范围 LPMRQO。 3. 利用递归方法分解各层子树,最终获得中序遍历结果为:`LPORMQN`。 --- #### 题目四 **描述**: 有一棵二叉树,它的中序遍历为 `ABCDEFG`,后序遍历为 `BDCAFEG`,请求出这棵二叉树的先序遍历。 **解答思路**: 1. 后序遍历的最后一个字符 G 表明它是整棵树的根节点[^3]。 2. 查找 G 在中序遍历的位置,将其分为两部分 ABCDEF(左子树)和空集(无右子树)。 3. 类似方式继续拆分直到完成全部层次划分,最终得到先序遍历结果为:`GFEDCBA`。 --- ```python # Python 实现辅助函数用于验证答案 class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def build_tree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root_index_inorder = inorder.index(root_val) root = TreeNode(root_val) root.left = build_tree(preorder[1:root_index_inorder+1], inorder[:root_index_inorder]) root.right = build_tree(preorder[root_index_inorder+1:], inorder[root_index_inorder+1:]) return root def post_order_traversal(root): result = [] stack = [(root, False)] while stack: node, visited = stack.pop() if node is None: continue if visited: result.append(node.val) else: stack.append((node, True)) stack.append((node.right, False)) stack.append((node.left, False)) return result ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值