各种遍历顺序如下图所示:

树的最大深度 

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def maxdepth(self, root):
if root is None:
return 0
return max(self.maxdepth(root.left), self.maxdepth(root.right))+1

深度优先

深度优先遍历有三种方式:前序遍历、中序遍历和后序遍历

所说的前序、中序、后序,是指根节点的先后顺序。

前序遍历:根节点 -> 左子树 -> 右子树

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def preorder(self, root):
if root is None:
return ''
print root.val
if root.lef:
self.preorder(root.left)
if root.right:
self.preorder(root.right)

中序遍历:左子树 -> 根节点 -> 右子树

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def midorder(self, root):
if root is None:
return ''
if root.lef:
self.midorder(root.left)
print root.val
if root.right:
self.midorder(root.right)

后序遍历:左子树 -> 右子树 -> 根节点

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def endorder(self, root):
if root is None:
return ''
if root.lef:
self.endorder(root.left)
if root.right:
self.endorder(root.right)
print root.val

广度优先

广度优先遍历,即层次遍历,优先遍历兄弟节点

层次遍历:根节点 -> 左节点 -> 右节点

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
  def graorder(self, root):
    if root is None:
      return ''
    queue = [root]
    while queue:
      res = []
      for item in queue:
        print item.val,
        if item.left:
          res.append(item.left)
        if item.right:
          res.apppend(item.right)
      queue = res

比较两棵树是否相同

# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def issame(self, root1, root2):
if root1 is None and root2 is None:
return True
elif root1 and root2:
return root1.val==root2.val and issame(root1.left, root2.left) and issame(root1.right, root2.right)
else:
return False

Python:树的遍历的更多相关文章

  1. 用python讲解数据结构之树的遍历

    树的结构 树(tree)是一种抽象数据类型或是实现这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合 它具有以下的特点: ①每个节点有零个或多个子节点: ②没有父节点的节点称为根节点: ③ ...

  2. Python中树的遍历-堆排序

    1.二叉树的遍历 遍历:迭代所有元素一遍. 树的遍历:对树中所有的元素不重复的访问一遍,也成扫描 广度优先遍历:层序遍历 深度优先遍历:前序.中序.后续遍历. 遍历序列:将树中所有元素遍历一遍后,得到 ...

  3. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  4. YTU 3023: 树的遍历

    原文链接:https://www.dreamwings.cn/ytu3023/2617.html 3023: 树的遍历 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 2 题 ...

  5. 团体程序设计天梯赛-练习集L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  6. leetcode404-----简单的树的遍历

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  7. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  8. L2-006. 树的遍历

    题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...

  9. js实现对树深度优先遍历与广度优先遍历

    深度优先与广度优先的定义 首先我们先要知道什么是深度优先什么是广度优先. 深度优先遍历是指从某个顶点出发,首先访问这个顶点,然后找出刚访问这个结点的第一个未被访问的邻结点,然后再以此邻结点为顶点,继续 ...

  10. L2-006 树的遍历 (25 分) (根据后序遍历与中序遍历建二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456 L2-006 树的遍历 (25 分 ...

随机推荐

  1. 成都Uber优步司机奖励政策(3月18日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. day 5 名片管理系统-文件版

    1.添加__name__ == '__main__' if __name__ == "__main__": #添加__name__变量 #调用主函数 main() 2.添加6功能, ...

  3. [转帖]localhost与127.0.0.1的区别

    localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...

  4. Arduino-元件简介

    DS18B20温度传感器 DS18B20是DALLAS公司生产的一种常用的温度传感器,其具有体积小巧.硬件功耗低.抗干扰能力强.精准度高的特点.该传感器具有单总线通讯的能力,电压范围为3.0V~5.5 ...

  5. Selenium(Python) ddt数据驱动

    首先, 添加ddt模块: import unittestfrom time import sleep from ddt import ddt, data, unpack# 导入ddt模块from se ...

  6. git配置github链接

    1.百度git官网-下载最新版git 2.一路默认下一步安装 3.打开 git bash here 命令行 4.注册github账号(用自己的邮箱就可以,不会英文可以用谷歌翻译)注册成功后建立项目 5 ...

  7. Unity Lighting - Choosing a Rendering Path 选择渲染路径(三)

      Choosing a Rendering Path 选择渲染路径 Unity supports a number of rendering techniques, or ‘paths’. An i ...

  8. git push origin master 错误解决办法

    一.错误代码如下: error: failed to push some refs to 'https://github.com/wbingithub/drag.git' 二.在网上搜了一下,如下写就 ...

  9. 【转载】图解Java常用数据结构(一)

    图解Java常用数据结构(一)  作者:大道方圆 原文:https://www.cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, 系统化看了下Jav ...

  10. Python高级编程-序列化

    在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict: dict1 = {'name': 'Rob', 'age': 19, 'score': 90} 可以随时修改变量,比如把age改成 ...