145 Binary Tree Postorder Traversal 二叉树的后序遍历

给定一棵二叉树,返回其节点值的后序遍历。
例如:
给定二叉树 [1,null,2,3],
   1
    \
     2
    /
   3
返回 [3,2,1]。
注意: 递归方法很简单,你可以使用迭代方法来解决吗?
详见:https://leetcode.com/problems/binary-tree-postorder-traversal/description/

方法一:递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root==nullptr)
        {
            return res;
        }
        return postorderTraversalHelper(root,res);
    }
    vector<int> postorderTraversalHelper(TreeNode* root,vector<int> &res)
    {
        if(root==nullptr)
        {
            return res;
        }
        postorderTraversalHelper(root->left,res);
        postorderTraversalHelper(root->right,res);
        res.push_back(root->val);
        return res;
    }
};

方法二:非递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root==nullptr)
        {
            return res;
        }
        stack<TreeNode*> stk1;
        stack<TreeNode*> stk2;
        stk1.push(root);
        while(!stk1.empty())
        {
            root=stk1.top();
            stk1.pop();
            stk2.push(root);
            if(root->left)
            {
                stk1.push(root->left);
            }
            if(root->right)
            {
                stk1.push(root->right);
            }
        }
        while(!stk2.empty())
        {
            root=stk2.top();
            stk2.pop();
            res.push_back(root->val);
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8727353.html

时间: 2024-10-15 12:04:40

145 Binary Tree Postorder Traversal 二叉树的后序遍历的相关文章

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

[LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序

Binary Tree Postorder Traversal 二叉树的后序遍历

地址:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题意就是完成二叉树的后序遍历,我们知道如果使用递归进行二叉树后序遍历将是非常简单的事情. public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer > ans = new ArrayList<>(); Tr

[LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? > read

lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历

题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val

[LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? http://www.cnblogs.com/dolphin0520/archive/201

leetcode 94.Binary Tree Inorder Traversal 二叉树的中序遍历

递归算法C++代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> in

Leetcode #145 Binary Tree Postorder Traversal

题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/ (非递归实现)二叉树的后序遍历. 1 class Solution 2 { 3 public: 4 vector<int> postorderTraversal(TreeNode *root) 5 { 6 vector<int> output; 7 if(root == NULL) 8 { 9 return output; 10 } 11 12 s

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur