zoukankan      html  css  js  c++  java
  • LeetCode:145. 二叉树的后序遍历

    1、题目描述

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

    示例:

    输入: [1,null,2,3]  
       1
        
         2
        /
       3 
    
    输出: [3,2,1]
    

    进阶: 递归算法很简单,你可以通过迭代算法完成吗?

    2、题解

    2.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 postorder(self, res,root):
            """递归实现后续遍历"""
            if root == None:
                return
            self.postorder(res, root.left)
            self.postorder(res, root.right)
            print(root.val)
            res.append(root.val)
            
            
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            res = []
            self.postorder(res,root)
    
            return res
    

      

  • 相关阅读:
    JUC并发工具包之Semaphore
    Linux命令
    uWSGI
    数据库 MySQL 练习
    c++
    c++ 初阶
    Git
    MySQl 和 Redis
    MySQL 查询
    MySQL 命令
  • 原文地址:https://www.cnblogs.com/bad-robot/p/10065474.html
Copyright © 2011-2022 走看看