二叉树问题---二叉树的前中后序的递归,非递归及Morris遍历
来源:互联网 发布:网络直播间 编辑:程序博客网 时间:2024/06/07 23:09
class TreeNode: def __init__(self, x): root.val = x root.left = None root.right = None
创建二叉树
def create_tree(root): element = input("Enter a key: ") if element == '#': root = None else: root = TreeNode(element) root.left = create_tree(root.left) root.right = create_tree(root.right) return root
前序遍历
def pre_order(root): if root: print(root.val,end=' ') pre_order(root.left) pre_order(root.right)
中序遍历
def mid_order(root): if root: mid_order(root.left) print(root.val, end=' ') mid_order(root.right)
后序遍历
def post_order(root): if root: post_order(root.left) post_order(root.right) print(root.val, end=' ')
非递归前序遍历
def preorder(root): if not root: return stack = [] while root or len(stack): if root: stack.append(root) print(root.val, end=' ') root = root.left else: root = stack.pop() root = root.right
非递归中序遍历
def midorder(root): if not root: return stack = [] while root or stack: if root: stack.append(root) root = root.left else: root = stack.pop() print(root.val, end=' ') root = root.right
非递归后序遍历
def postorder(root): if not root: return stack1 = [] stack2 = [] while root or stack1: if root: stack1.append(root) stack2.append(root.val) root = root.right else: root = stack1.pop() root = root.left while stack2: print(stack2.pop(), end=' ')
非递归后序遍历(使用一个栈)
说明:使用一个变量c来记录每次弹出的元素,保证元素不会重复进栈
def postOrder(root): if not root: return stack = [] stack.append(root) c = None while stack: c = stack[-1] if c.left and c.left != root and c.right != root: stack.append(c.left) elif c.right and c.right != root: stack.append(c.right) else: print(stack.pop().val, end=' ') root = c
Morris前序遍历
def morrisPre(root): if not root: return while root: cur = root.left if cur: while cur.right and cur.right != root: cur = cur.right if not cur.right: cur.right = root print(root.val, end=' ') root = root.left continue else: cur.right = None else: print(root.val, end=' ') root = root.right
Morris中序遍历
def morrisIn(root): if not root: return while root: cur = root.left if cur: while cur.right and cur.right != root: cur = cur.right if not cur.right: cur.right = root root = root.left continue else: cur.right = None print(root.val, end=' ') root = root.right
Morris后序遍历
def morrisPost(root): def printRightEdge(root): tail = reverseEdge(root) cur = tail while cur: print(cur.val, end=' ') cur = cur.right reverseEdge(tail) def reverseEdge(root): pre = None while root: next = root.right root.right = pre pre = root root = next return pre if not root: return head = root while root: cur = root.left if cur: while cur.right and cur.right != root: cur = cur.right if not cur.right: cur.right = root root = root.left continue else: cur.right = None printRightEdge(root.left) root = root.right printRightEdge(head)
层次遍历
def levelorder(root): if not root: return queue = [] queue.append(root) while queue: root = queue.pop(0) print(root.val, end=' ') if root.left: queue.append(root.left) if root.right: queue.append(root.right)
阅读全文
2 0
- 二叉树问题---二叉树的前中后序的递归,非递归及Morris遍历
- 【树】二叉树的非递归遍历(栈&Morris)
- 二叉树的非递归遍历(Morris Traversal)
- 二叉树遍历(递归、非递归、Morris遍历)
- 二叉树遍历(递归、非递归、Morris遍历)
- 二叉树的三种遍历方式(递归、非递归和Morris遍历)
- 二叉树的三种非递归遍历和morris遍历
- 二叉树的递归,非递归遍历
- 二叉树的递归+非递归遍历
- 二叉树的递归非递归遍历
- 二叉树的遍历--递归+非递归
- 二叉树的递归、非递归遍历
- 二叉树的递归非递归遍历
- 二叉树的创建及递归和非递归遍历
- 二叉树的inorder实现:递归、栈、Morris遍历
- Morris Traversal遍历二叉树(非递归)
- 二叉树的建立及层次遍历及前中后序遍历递归与非递归的实现
- 二叉树递归及非递归遍历
- Docker学习笔记(一) 简介
- webpack+vue 开发app(一)
- Oracle sql 的使用细节 和 Oracle和MySQL的区别
- opencv3.0 官网地址
- 十分钟搞定pandas
- 二叉树问题---二叉树的前中后序的递归,非递归及Morris遍历
- GIT error: The following untracked working tree files would be overwritten…
- less命令
- 列表的基本操作 of Python
- MySQL 系列第二天
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
- 【牛腩新闻发布系统】——CSS
- placeholder样式设置
- 导航栏标题