leetcode || 145、Binary Tree Postorder Traversal

problem:

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?

Hide Tags

Tree Stack

题意:非递归后续遍历二叉树

thinking:

(1)非递归后续遍历二叉树的方法比较抽象,要借助两个stack

(2)采用双stack倒换,第一个stack出一次栈,将两个孩子(先左后右)入栈,出栈的节点保存到第二个stack。对第二个stack依次出栈即得到后续遍历的结果。

code:

class Solution {
  public:
      vector<int> postorderTraversal(TreeNode* root) {
          stack<TreeNode *> input;
          stack<TreeNode *> output;
          vector<int> ret;
          if(root==NULL)
            return ret;
          TreeNode *node = root;
          input.push(node);
          while(!input.empty())
          {
              node=input.top();
              input.pop();
              output.push(node);
              if(node->left!=NULL)
                  input.push(node->left);
              if(node->right!=NULL)
                  input.push(node->right);
          }
          while(!output.empty())
          {
              node=output.top();
              output.pop();
              ret.push_back(node->val);
          }
          return ret;
      }
  };
时间: 2024-08-08 13:38:22

leetcode || 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 import java.uti

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal II

Binary Tree Level Order Traversal II Total Accepted: 16983 Total Submissions: 54229My Submissions Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For ex

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal

Binary Tree Level Order Traversal Total Accepted: 20571 Total Submissions: 66679My Submissions Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,2

leetcode || 144、Binary Tree Preorder Traversal

problem: 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? Hide Tags Tree Stack 题意:非递归前序遍历二叉树 t

leetcode || 94、Binary Tree Inorder Traversal

problem: 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?

LeetCode: Binary Tree Postorder Traversal [145]

[题目] 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】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

[Leetcode][Tree][Binary Tree Postorder Traversal]

二叉树的后续遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsPostorderTraversal(TreeNode *now, vec

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)复杂度分析:时