zoukankan      html  css  js  c++  java
  • LeetCode

    Binary Tree Postorder Traversal

    2014.1.14 21:17

    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?

    Solution1:

      Recursive solution is trivial, here is the trivial code.

      Time and space complexities are both O(n) scale.

    Accepted code:

     1 // 1AC, the trivial recursive version.
     2 class Solution {
     3 public:
     4     vector<int> postorderTraversal(TreeNode *root) {
     5         result.clear();
     6         
     7         if(root == nullptr){
     8             return result;
     9         }
    10         
    11         preorderTraversalRecursive(root);
    12         
    13         return result;
    14     }
    15 private:
    16     void preorderTraversalRecursive(TreeNode *root) {
    17         if(root == nullptr){
    18             return;
    19         }
    20         preorderTraversalRecursive(root->left);
    21         preorderTraversalRecursive(root->right);
    22         result.push_back(root->val);
    23     }
    24     vector<int> result;
    25 };

    Solution2:

      Here is the iterative version. Two extra stacks are used to track the traversal. One used to record TreeNode pointer, the other used to record its counting. The counting here has the following meaning:

        0: the node has just been pushed into stack. The node is new.

        1: the left child of the node is in stack.

        2: the right child of the node is in stack. The node will be popped out when it is on top of stack.

      Time and space complexites are still O(n).

    Accepted code:

     1 // 2WA, 1AC, should've think more before starting coding
     2 class Solution {
     3 public:
     4     vector<int> postorderTraversal(TreeNode *root) {
     5         result.clear();
     6         
     7         if(root == nullptr){
     8             return result;
     9         }
    10         
    11         vstack.clear();
    12         cstack.clear();
    13         vstack.push_back(root);
    14         cstack.push_back(0);
    15         while(vstack.size() > 0){
    16             if(cstack[cstack.size() - 1] == 0){
    17                 ++cstack[cstack.size() - 1];
    18                 if(vstack[vstack.size() - 1]->left != nullptr){
    19                     cstack.push_back(0);
    20                     vstack.push_back(vstack[vstack.size() - 1]->left);
    21                 }
    22             }else if(cstack[cstack.size() - 1] == 1){
    23                 ++cstack[cstack.size() - 1];
    24                 if(vstack[vstack.size() - 1]->right != nullptr){
    25                     cstack.push_back(0);
    26                     vstack.push_back(vstack[vstack.size() - 1]->right);
    27                     // 1WA here, cannot push result here
    28                     // result.push_back(vstack[vstack.size() - 1]->val);
    29                     // 1WA here, cannot replace node here, this works for preorder, but not postorder
    30                     // vstack[vstack.size() - 1] = vstack[vstack.size() - 1]->right;
    31                 }
    32             }else{
    33                 result.push_back(vstack[vstack.size() - 1]->val);
    34                 cstack.pop_back();
    35                 vstack.pop_back();
    36             }
    37         }
    38         
    39         return result;
    40     }
    41 private:
    42     vector<int> result;
    43     vector<TreeNode *> vstack;
    44     vector<int> cstack;
    45 };
  • 相关阅读:
    JavaScript深复制
    Javascript Date Format
    SqlHelper include Transaction
    JQuery 对 Select option 的操作---转载
    转载:SQL索引一步到位
    转载 SQL Server 2008 R2 事务与隔离级别实例讲解
    使用Log Explorer查看和恢复数据
    SqlServer正在执行的sql语句
    SQL Server Reporting Services – Insufficient Rights Error
    asp.net 间传值的方法
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3519809.html
Copyright © 2011-2022 走看看