首页 > 编程语言 > 详细

Leetcode练习(Python):栈类:第145题:二叉树的后序遍历:给定一个二叉树,返回它的 后序 遍历。

时间:2020-05-15 20:50:27      阅读:40      评论:0      收藏:0      [点我收藏+]

题目:

二叉树的后序遍历:给定一个二叉树,返回它的 后序 遍历。

思路:

递归大法好,之后补充使用栈来实现的。

程序1:递归实现

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        result = []
        def postorder(root):
            if root == None:
                return result
            postorder(root.left)
            postorder(root.right)
            result.append(root.val)
        postorder(root)
        return result

  

Leetcode练习(Python):栈类:第145题:二叉树的后序遍历:给定一个二叉树,返回它的 后序 遍历。

原文:https://www.cnblogs.com/zhuozige/p/12896638.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!