c 打印二叉树_数据结构与算法二叉树篇(上)

一、定义树的数据结构

####树的结构定义class TreeNode:    def_init_(self,x):        self.val=x        self.right=None        self.left=None

二、树的前序,中序,后序遍历递归解法

2-1. 前序遍历:

###树的前序递归遍历############def preorder(root):    if not root:return     print(root.val)    self.preorder(root.left)    self.preorder(root.right)

2-2. 中序遍历:

###树的中序递归遍历############def inorder(root):    if not root:return     self.inorder(root.left)    print(root.val)    self.inorder(root.right)

2-3.后序遍历:

###树的后序递归遍历###########def postorder(root):    if not root:return     self.postorder(root.left)    self.postorder(root.right)    print(root.val)

三、前序,中序,后序的非递归解法

3-1.树的前序非递归方法遍历

######思路:##1.申请一个栈stack,并将头结点(根节点)压入栈中##2.弹出stack栈中的栈顶元素,并标记为temp(temp=stack.pop()).##接下来####打印temp,然后依次压入temp的右孩子,最后压入左孩子。##3.循环2的过程,直到stack为空#######python代码:    if not root:return        stack=[root]    while stack:        s=stack.pop()        if s:            print(s.val)            stack.append(s.right)            stack.append(s.left)

3-2  树的中序遍历非递归方法

##思路##1.申请一个新的栈stack,并且令temp=root##2.先把temp压入栈中,对以temp节点为根的整棵树,###依次把左边界压入##栈中(即不停地做stack.append(temp),temp=temp.left),###直到##temp.left=none时截止##3.不断重复步骤2,直到temp.left=none时截止。###此时弹出栈顶元素,并且##记为node.打印node值,并且令temp=node.right,###然后继续进行步骤2##4.当stack和temp为空,则整个程序停止def inorder(root):    if not root:return     stack = []    temp = root    while stack or temp:        while temp:            stack.append(temp)            temp=temp.left        node=stack.pop()        print(node.val)        temp=node.right

3-3 树的后序遍历非递归方法

##思路##1.申请一个栈记为stack,将头结点压入栈中,并且设置两个变量h,c;##在处理流程中h代表最近一次弹出并打印的节点,c代表stack栈顶节点;##初始化h为头结点,c为null##2.每次令c为当前栈的栈顶元素,但不从栈顶弹出。此时分为三种情况:# ①如果c的左孩子不为null,并且h不等于c的左孩子,也不等于c的右孩子。#则把c的左孩子压入栈中# ②如果条件①不成立,即c的右孩子不为null,并且h不等于c的右孩子,#则把c的右孩子压入栈中# ③如果条件①和②都不成立,说明c的左右孩子都打印结束,#则弹出c,并且令h=c##3.一直重复过程2,直到stack为空###代码:def postorder(root):    if not root:return     stack=[]    temp=root    stack=[temp]    h,c=temp,None    while stack:        c=stack[-1]        if c.left and h!=c.left and h!=c.right:            stack.append(c.left)        elif c.right and h!=c.right:            stack.append(c.right)        else:            stack.pop()            print(c.val)

四、层序遍历的三种常考题型

1.依次打印二叉树的每一个节点并保存在一个数组中输出。

###思路:###1.申请一个队列,并将头结点压入队列###2.求出队列的长度。在队列长度范围内,将队列的首元素出队列并输出###值。###判断出队列的节点有没有孩子节点,有则压入。###3.步骤2循环至队列为空def BFS(root):    if not root:        return     queue = [root]    while queue:        n = len(queue)        for i in range(n):            q = queue.pop(0)            if q:                print(q.val)                queue.append(q.left if q.left else None)                queue.append(q.right if q.right else None)

4-2:从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行.

 def BFS(root):     if not root:         return []     temp = root     queue = [root]     res = []     while queue:         arr = []         n = len(queue)         for i in range(n):             node = queue.pop(0)             if node:                 arr.append(node.val)                 if node.left:                     queue.append(node.left)                 if node.right:                     queue.append(node.right)         res.append(arr)     print(res)

4-3 :按照之字形打印二叉树

def BFS():    if not root:        return []    res=[]   #输出结果    queue=[root]    count=0    while queue:        temp=[]        n=len(queue)        count=count+1        for _ in range(n):            node=queue.pop(0)            temp.append(node.val)            if node.left:                queue.append(node.left)            if node.right:                queue.append(node.right)            if count % 2:                res.append(temp)            else:                res.append(temp[::-1])    return res

首次推出公众号文章,难免有一些错误,希望大家理解。

温馨提示:整理不易,转载请注明出处。欢迎大家交流以及补充其他语言代码。38294c05535f57e76ee11d957ba51d3a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值