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: Recursive solution is trivial, could you do it iteratively?

分析

直接上代码……

vector<int> postorderTraversal(TreeNode* root) {
    if (root != NULL) {
        postorderTraversal(root->left);
        postorderTraversal(root->right);
        v.push_back(root->val);
    }
    return v;
}
/**
* 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> v;

    void  postorderTraversalIter(TreeNode *root, stack<TreeNode*> &stac) {
        if (root == NULL) return;
        bool hasLeft = root->left != NULL;
        bool hasRight = root->right != NULL;
        stac.push(root);
        if (hasRight)
            stac.push(root->right);
        if (hasLeft)
            stac.push(root->left);
        if (!hasLeft && !hasRight)
            v.push_back(root->val);
        if (hasLeft) {
            root = stac.top();
            stac.pop();
            postorderTraversalIter(root, stac);
        }
        if (hasRight) {
            root = stac.top();
            stac.pop();
            postorderTraversalIter(root, stac);
        }
        if (hasLeft || hasRight)
            v.push_back(stac.top()->val);
        stac.pop();
    }

    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> stac;
        postorderTraversalIter(root, stac);
        return v;
    }
};

另有两道类似的题目:

LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)

LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

时间: 2024-12-26 13:06:36

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

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

Java for LeetCode 145 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]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

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题解: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? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

[LeetCode][JavaScript]Binary Tree Postorder Traversal

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? 同样的配方,同样的味道.

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

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? 解法一:递归法 /** *

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions 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 s

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree