Python实现二叉树的左中右序遍历

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/3/18 12:31
# @Author : baoshan
# @Site :
# @File : binarytree.py
# @Software: PyCharm Community Edition # python 实现二叉树的左中右序遍历 class Node(object):
def __init__(self, index):
self.index = index
self.left_child = None
self.right_child = None class BinaryTree(Node):
def __init__(self, root):
self.root = root def pre_travel(self, node):
if not node:
return
print(node.index)
self.pre_travel(node.left_child)
self.pre_travel(node.right_child) def mid_travel(self, node):
if not node:
return
self.mid_travel(node.left_child)
print(node.index)
self.mid_travel(node.right_child) def suf_travel(self, node):
if not node:
return
self.suf_travel(node.left_child)
self.suf_travel(node.right_child)
print(node.index) node_dict = {}
for i in range(1, 10):
node_dict[i] = Node(i) node_dict[1].left_child = node_dict[2]
node_dict[1].right_child = node_dict[3]
node_dict[2].left_child = node_dict[5]
node_dict[2].right_child = node_dict[6]
node_dict[3].left_child = node_dict[7]
node_dict[7].left_child = node_dict[8]
node_dict[7].right_child = node_dict[9] tree = BinaryTree(node_dict[1])
print('---左序遍历---')
tree.pre_travel(tree.root)
print('---中序遍历---')
tree.mid_travel(tree.root)
print('---右序遍历---')
tree.suf_travel(tree.root)
输出结果:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/PycharmProjects/myProject/python_weixin_study/binarytree.py
---左序遍历---
1
2
5
6
3
7
8
9
---中序遍历---
5
2
6
1
8
7
9
3
---右序遍历---
5
6
2
8
9
7
3
1 Process finished with exit code 0
请各位大虾指教!
Python实现二叉树的左中右序遍历的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
- [Swift]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- 二叉树 排序二叉树-可以通过中序遍历得到排序的数据 二叉排序树时间复杂度O(logn),
二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加.但是他也有自己的缺点:删除操作复杂. 虽然二叉排序树的最坏效率是O(n),但它支持动 ...
- 数据结构-用C++实现一个二叉树,递归方法中序遍历
1:二叉排序树,又称二叉树.其定义为:二叉排序树或者空树,或者是满足如下性质的二叉树. (1)若它的左子树非空,则左子树上所有节点的值均小于根节点的值. (2)若它的右子树非空,则右子树上所有节点的值 ...
- 翻转二叉树(深搜-先序遍历-交换Node)
题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...
- C++二叉树前中后序遍历(递归&非递归)统一代码格式
统一下二叉树的代码格式,递归和非递归都统一格式,方便记忆管理. 三种递归格式: 前序遍历: void PreOrder(TreeNode* root, vector<int>&pa ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集
L2-006 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
随机推荐
- Debug 路漫漫-02
重现标准 BTL Model ,using MATLAB: 1. 错误使用 cat要串联的数组的维度不一致.出错 cell2mat (line 83) m{n} = cat(1,c{:,n}); —— ...
- HTTPS 指南
苹果在 WWDC 2016 上宣布:2016 年底将要求所有 APP 适配苹果的 App Transport Security,简单地说就是除了特殊情况(浏览器.第三方服务.媒体)外,APP 跟服务端 ...
- Redis学习之路(002)- Ubuntu下redis开放端口
Redis在ubuntu安装后默认是只有本地访问,需要别的ip访问我们需要修改redis的配置文件 1. dpkg -L redis-server 这命令我们可以看到redis的安装的文件在那些目录 ...
- 如何使用Git上传项目代码到github
这是我第一次应用git,以下仅供git的初学者参考. github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说 ...
- MFC坐标转换
1.GetWindowRect是取得窗口在屏幕坐标系下的RECT坐标(包括客户区和非客户区),这样可以得到窗口的大小和相对屏幕左上角(0,0)的位置. 2.GetClientRect取得窗口客户区(不 ...
- 使用itext直接替换PDF中的文本
直接说问题,itext没有直接提供替换PDF中文本的接口(查看资料得到的结论是PDF不支持这种操作),不过存在解决思路:在需要替换的文本上覆盖新的文本.按照这个思路我们需要解决以下几个问题: itex ...
- 【Spring】Spring之事务处理
编程式事务 /** * 1. 根据DataSource去创建事务管理器 * 构造方法 , 参数1. DataSource */ DataSourceTransactionManager txManag ...
- win7 32 安装mongoDB遇到的问题
net start MongoDB报错:发生服务特定错误: 100. 直接进入db文件夹,先删除 mongod.lock 文件,然后重新启动服务即可:要是还不行,就继续删 storage.bson文件 ...
- Python使用matplotlib模块绘制多条折线图、散点图
用matplotlib模块 #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:折线图.散点图测试 ''' import rando ...
- FreeSWITCH网关参数之caller-id-in-from
1. 这个配置项两个设置值: true和false(默认) <param name="caller-id-in-from" value="true"/&g ...