#!/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实现二叉树的左中右序遍历的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】

    [145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...

  2. [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 ...

  3. 二叉树 排序二叉树-可以通过中序遍历得到排序的数据 二叉排序树时间复杂度O(logn),

    二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加.但是他也有自己的缺点:删除操作复杂. 虽然二叉排序树的最坏效率是O(n),但它支持动 ...

  4. 数据结构-用C++实现一个二叉树,递归方法中序遍历

    1:二叉排序树,又称二叉树.其定义为:二叉排序树或者空树,或者是满足如下性质的二叉树. (1)若它的左子树非空,则左子树上所有节点的值均小于根节点的值. (2)若它的右子树非空,则右子树上所有节点的值 ...

  5. 翻转二叉树(深搜-先序遍历-交换Node)

    题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...

  6. C++二叉树前中后序遍历(递归&非递归)统一代码格式

    统一下二叉树的代码格式,递归和非递归都统一格式,方便记忆管理. 三种递归格式: 前序遍历: void PreOrder(TreeNode* root, vector<int>&pa ...

  7. Leetcode 94. 二叉树的中序遍历

    1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...

  8. PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集

    L2-006 树的遍历(25 分)   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...

  9. LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

随机推荐

  1. MATLAB 的输入输出命令

    MATLAB 的输入输出命令: >> A='woshi'; >> disp(A) woshi fscanf和fprintf命令的行为像C scanf和printf函数.他们支持 ...

  2. 【C++】不要依赖编译器的默认初始值

    最好在定义的时候就给出初始值. 类和结构体给出构造函数. 比如int,在vs的debug和release模式下,初始化的值是不同的.

  3. lsof命令详解(转)

    lsof命令详解(转) 上一篇 / 下一篇  2011-06-09 21:56:41 / 个人分类:Linux 查看( 351 ) / 评论( 0 ) / 评分( 0 / 0 ) 在Linux中,ls ...

  4. vim缩进参考线

    编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级.代码如下: ? ReferenceLine 1 2 3 4 5 6 7 8 9 10 11 ...

  5. Sentinel 简介与API订阅发布

    Sentinel 简介 Redis 的 Sentinel 系统用于管理多个 redis 服务器(instance), 该系统执行以下三个任务: 监控(Monitoring): Sentinel 会不断 ...

  6. Android权限详解

    在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里ATAAW.COM将各类访问权限一一罗列出来,供大家 ...

  7. 【Oracle】Oracle中常用的系统函数

    Oracle SQL 提供了用于执行特定操作的专用函数.这些函数大大增强了 SQL 语言的功能.函数可以接受零个或者多个输入参数,并返回一个输出结果.在Oracle还可以自定义函数,关于更多信息可以查 ...

  8. 【java】详解I/O流

    目录结构: contents structure [+] File类 I/O流体系 流的基本介绍 访问文件 转化流 DataInputStream和DataOutputStream 对象流 推回输入流 ...

  9. IntelliJ IDEA 最新激活码(截止到2018年10月14日)

    IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYX ...

  10. Markdown 使用教程

    前言 以前经常在 github 中看到 .md 格式的文件,一直没有注意,也不明白为什么文本文档的后缀不是 .txt ,后来无意中看到了 Markdown,看到了用这个东西写得一些web界面等特别的规 ...