• LeetCode


    Binary Tree Preorder Traversal

    2014.1.14 02:36

    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?

    Solution1:

      The recursive version of preorder traversal, everybody knows what to do.

      Time and space complexities are both O(n), where n is the number of nodes in the tree.

    Accepted code:

     1 // 1WA, 1RE, 1AC
     2 /**
     3  * Definition for binary tree
     4  * struct TreeNode {
     5  *     int val;
     6  *     TreeNode *left;
     7  *     TreeNode *right;
     8  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     9  * };
    10  */
    11 class Solution {
    12 public:
    13     vector<int> preorderTraversal(TreeNode *root) {
    14         // IMPORTANT: Please reset any member data you declared, as
    15         // the same Solution instance will be reused for each test case.
    16         result.clear();
    17         // 1WA here, this sentence missing
    18         preorder(root);
    19         return result;
    20     }
    21 private:
    22     vector<int> result;
    23     
    24     void preorder(TreeNode *root) {
    25         // 1RE here, if(root)
    26         if(root == nullptr){
    27             return;
    28         }
    29         result.push_back(root->val);
    30         preorder(root->left);
    31         preorder(root->right);
    32     }
    33 };

    Solution2:

      Iterative version, not so easy as the last one.

      Time and space complexities are still O(n).

    Accepted code:

     1 class Solution {
     2 public:
     3     vector<int> preorderTraversal(TreeNode *root) {
     4         result.clear();
     5         
     6         if(root == nullptr){
     7             return result;
     8         }
     9         
    10         vstack.clear();
    11         cstack.clear();
    12         vstack.push_back(root);
    13         cstack.push_back(0);
    14         result.push_back(root->val);
    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                     result.push_back(vstack[vstack.size() - 1]->val);
    22                 }
    23             }else if(cstack[cstack.size() - 1] == 1){
    24                 ++cstack[cstack.size() - 1];
    25                 if(vstack[vstack.size() - 1]->right != nullptr){
    26                     cstack[cstack.size() - 1] = 0;
    27                     vstack[vstack.size() - 1] = vstack[vstack.size() - 1]->right;
    28                     result.push_back(vstack[vstack.size() - 1]->val);
    29                 }
    30             }else{
    31                 cstack.pop_back();
    32                 vstack.pop_back();
    33             }
    34         }
    35         
    36         return result;
    37     }
    38 private:
    39     vector<int> result;
    40     vector<TreeNode *> vstack;
    41     vector<int> cstack;
    42 };
  • 相关阅读:
    delete
    js混淆代码还原-js反混淆:利用js进行赋值实现
    Microservice Trade-Offs
    tagged by: microservices 【martinfowler.com】
    Tackle Business Complexity in a Microservice with DDD and CQRS Patterns
    Why Can't I Access A Protected Member From A Derived Class
    tagged by: domain driven design 【martinfowler.com】
    Domain Driven Design
    Why Would I Ever Need to Use C# Nested Classes
    RabbitMQ compare with redis and MSMQ
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3518333.html
Copyright © 2020-2023  润新知