列表

详情


145. 二叉树的后序遍历

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历

 

示例 1:

输入:root = [1,null,2,3]
输出:[3,2,1]

示例 2:

输入:root = []
输出:[]

示例 3:

输入:root = [1]
输出:[1]

 

提示:

 

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

相似题目

二叉树的中序遍历

N 叉树的后序遍历

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> postorderTraversal(TreeNode* root) { } };

golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-07-22 10:14:00

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func postorderTraversal(root *TreeNode) (ans []int) {
    var dfs func(*TreeNode)
    dfs = func(node *TreeNode) {
        if node != nil {
            dfs(node.Left)
            dfs(node.Right)
            ans = append(ans, node.Val)
        }
    }

    dfs(root)
    return
}

php 解法, 执行用时: 8 ms, 内存消耗: 15.3 MB, 提交时间: 2021-06-17 10:28:48

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Integer[]
     */
    function postorderTraversal($root) {
        $stack = [];
        $res = [];
        if ( $root != null )
            $stack[] = $root;
        
        while ( !empty($stack) ) {
            $node = array_pop($stack);
            if ( $node != null ) {
                $stack[] = $node;
                $stack[] = null;
                if ( $node->right ) $stack[] = $node->right;
                if ( $node->left ) $stack[] = $node->left;
            } else {
                $node = array_pop($stack);
                $res[] = $node->val;
            }
        }
        return $res;
    }
}

python3 解法, 执行用时: 44 ms, 内存消耗: 15 MB, 提交时间: 2021-06-17 10:22:19

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        res = []
        stack = []
        if root:
            stack.append(root)
        while stack:
            node = stack[-1]
            if node:
                stack.pop()
                # 父节点先入栈
                stack.append(node)
                # 空节点入栈
                stack.append(None)
                if node.right:  # 右节点入栈
                    stack.append(node.right)
                if node.left: # 左节点入栈
                    stack.append(node.left)
            else:
                stack.pop()  # 空节点出栈
                node = stack.pop()  # 父节点出栈
                res.append(node.val)
        return res

上一题