博客
关于我
数据结构 python3 二叉树遍历 前序 中序 后序
阅读量:341 次
发布时间:2019-03-04

本文共 866 字,大约阅读时间需要 2 分钟。

在这里插入图片描述

前序[0, 1, 3, 4, 2]

# Definition for a binary tree node.from typing import Listclass TreeNode:    def __init__(self, val=0, left=None, right=None):        self.val = val        self.left = left        self.right = rightclass Solution:    def inorderTraversal(self, root: TreeNode) -> List[int]:        res = []        def dfs(root):            if not root:                return            res.append(root.val)            dfs(root.left)            dfs(root.right)        dfs(root)        return resroot = TreeNode(0)lc = TreeNode(1)rc = TreeNode(2)lc_lc = TreeNode(3)lc_rc = TreeNode(4)root.left = lcroot.right = rclc.left=lc_lclc.right=lc_rcprint(Solution().inorderTraversal(root))

中序 [3, 1, 4, 0, 2]

dfs(root.left)            res.append(root.val)            dfs(root.right)

后序 [3, 4, 1, 2, 0]

dfs(root.left)            dfs(root.right)            res.append(root.val)

转载地址:http://qdce.baihongyu.com/

你可能感兴趣的文章
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
ng6.1 新特性:滚回到之前的位置
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(一)—— 组件介绍
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
nginx - thinkphp 如何实现url的rewrite
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>