今日头条面试题,先做下:

二叉树代码实现

class Node:
def __init__(self,item):
self.item = item
self.child1 = None
self.child2 = None class Tree:
def __init__(self):
self.root = None def add(self, item):
node = Node(item)
if self.root is None:
self.root = node
else:
q = [self.root] while True:
pop_node = q.pop(0)
if pop_node.child1 is None:
pop_node.child1 = node
return
elif pop_node.child2 is None:
pop_node.child2 = node
return
else:
q.append(pop_node.child1)
q.append(pop_node.child2) def traverse(self): # 层次遍历
if self.root is None:
return None
q = [self.root]
res = [self.root.item]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 is not None:
q.append(pop_node.child1)
res.append(pop_node.child1.item) if pop_node.child2 is not None:
q.append(pop_node.child2)
res.append(pop_node.child2.item)
return res def preorder(self,root): # 先序遍历
if root is None:
return []
result = [root.item]
left_item = self.preorder(root.child1)
right_item = self.preorder(root.child2)
return result + left_item + right_item def inorder(self,root): # 中序序遍历
if root is None:
return []
result = [root.item]
left_item = self.inorder(root.child1)
right_item = self.inorder(root.child2)
return left_item + result + right_item def postorder(self,root): # 后序遍历
if root is None:
return []
result = [root.item]
left_item = self.postorder(root.child1)
right_item = self.postorder(root.child2)
return left_item + right_item + result t = Tree()
for i in range(10):
t.add(i)
print('层序遍历:',t.traverse())
print('先序遍历:',t.preorder(t.root))
print('中序遍历:',t.inorder(t.root))
print('后序遍历:',t.postorder(t.root))

python_二叉树简单实现的更多相关文章

  1. python二叉树简单实现

    二叉树简单实现: class Node: def __init__(self,item): self.item = item self.child1 = None self.child2 = None ...

  2. 【Leetcode】二叉树简单路径最大和问题

    问题一:二叉树任意两个叶子间简单路径最大和 示例: -100 /   \ 2   100 /  \ 10   20 思路:这个问题适用于递归思路. 首先,将问题简单化:假设包含最大和summax的简单 ...

  3. Python_二叉树

    BinaryTree.py '''二叉树:是每个节点最多有两个子树(分别称为左子树和右子树)的树结构,二叉树的第i层最多有2**(i-1)个节点,常用于排序或查找''' class BinaryTre ...

  4. Python_字符串简单加密解密

    def crypt(source,key): from itertools import cycle result='' temp=cycle(key) for ch in source: resul ...

  5. c实现二叉树

    C实现二叉树 简单说明 实现了先序遍历.中序遍历.后序遍历.搜索 本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就 ...

  6. STL库学习笔记(待补充QAQ

    几百年前就说要学STL了然后现在还没动呢QAQ总算还是克服了懒癌决定学下QAQ 首先港下有哪几个vector listset map stack queue,大概就这些? 然后因为有几个基本操作每个都 ...

  7. leetcode树专题894.897,919,951

    满二叉树是一类二叉树,其中每个结点恰好有 0 或 2 个子结点. 返回包含 N 个结点的所有可能满二叉树的列表. 答案的每个元素都是一个可能树的根结点. 答案中每个树的每个结点都必须有 node.va ...

  8. LeetCode 题解目录

    前言 本目录将不断更新记录leetcode的刷题日记. 二叉树 序号 标题 难度 标签 1 108 将有序数组转换为二叉搜索树 简单 树.深度优先搜索 2 538 把二叉搜索树转换为累加树 简单 树 ...

  9. 数据库索引的基石----B树

    数据结构相对来说比较枯燥, 我尽量用最易懂的话,来把B树讲清楚.学过数据结构的人都接触过一个概念二叉树,简单来说,就是每个父节点最多有两个子节点.为了在二叉树上更快的进行元素的查找,人们通过不断的改进 ...

随机推荐

  1. HAVING 搜索条件在进行分组操作之后应用

    HAVING 搜索条件在进行分组操作之后应用: 如:查询帖子访问量大于15的用户id: select t.user_id,u.name,sum(count_view) from t_topic t l ...

  2. 【后缀数组】【LuoguP2408】 不同子串个数

    题目链接 题目描述 给你一个长为N的字符串,求不同的子串的个数 我们定义两个子串不同,当且仅当有这两个子串长度不一样 或者长度一样且有任意一位不一样. 子串的定义:原字符串中连续的一段字符组成的字符串 ...

  3. 原创:史上对BM25模型最全面最深刻的解读以及lucene排序深入讲解

    垂直搜索结果的优化包括对搜索结果的控制和排序优化两方面,其中排序又是重中之重.本文将全面深入探讨垂直搜索的排序模型的演化过程,最后推导出BM25模型的排序.然后将演示如何修改lucene的排序源代码, ...

  4. RESTFull开发风格

  5. VUE中常用的十大过滤器

    在vue的学习过程中,我发现过滤器是一个很好用的工具,过滤器(Filters)来渲染数据是一种很有趣的方式.过滤器不能替代Vue中的methods.computed或者watch,不改变真正的data ...

  6. IIS 7中添加FTP站点并设置指定用户访问

    1. 开启 FTP 和 IIS 服务功能:   2. 添加新用户: 打开计算机管理界面:   展开“本地用户与组”:   邮件点击“用户”->点击“创建新用户”:   在创建新用户对话框输入用户 ...

  7. Python面向对象编程核心思想

    原文地址https://blog.csdn.net/weixin_42134789/article/details/80194788 https://blog.csdn.net/happyjxt/ar ...

  8. layui的layer.open()方法查看缩略图 原图缩放

    写在前面 需求是页面上的图片缩略图, 鼠标悬浮时显示原图片, 并按比例缩放. 操作步骤 官方文档 点击跳转 关键属性 1. type: 设置type=1, 以页面的形式展示图片 2.  content ...

  9. Android中Activity的启动模式(LaunchMode)和使用场景

    一.为什么需要启动模式在Android开发中,我们都知道,在默认的情况下,如果我们启动的是同一个Activity的话,系统会创建多个实例并把它们一一放入任务栈中.当我们点击返回(back)键,这些Ac ...

  10. unable to lock the administration错误解决

    在ubantu系统上,使用 apt-get 命令或者其相对更新的APT 管理工具时,遇到 unable to lock the administration directory (/var/lib/d ...