• [LintCode] 二叉树的中序遍历


    The recursive solution is trivial and I omit it here.

    Iterative Solution using Stack (O(n) time and O(n) space):

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14     /**
    15      * @param root: The root of binary tree.
    16      * @return: Inorder in vector which contains node values.
    17      */
    18 public:
    19     vector<int> inorderTraversal(TreeNode *root) {
    20         // write your code here
    21         vector<int> nodes;
    22         TreeNode* node = root;
    23         stack<TreeNode*> toVisit;
    24         while (node || !toVisit.empty()) {
    25             if (node) {
    26                 toVisit.push(node);
    27                 node = node -> left;
    28             }
    29             else {
    30                 node = toVisit.top();
    31                 toVisit.pop();
    32                 nodes.push_back(node -> val);
    33                 node = node -> right;
    34             }
    35         }
    36         return nodes;
    37     }
    38 };

    Another more sophisticated soltuion using Morris Traversal (O(n) time and O(1) space):

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14     /**
    15      * @param root: The root of binary tree.
    16      * @return: Inorder in vector which contains node values.
    17      */
    18 public:
    19     vector<int> inorderTraversal(TreeNode *root) {
    20         // write your code here
    21         vector<int> nodes;
    22         TreeNode* node = root;
    23         while (node) {
    24             if (node -> left) {
    25                 TreeNode* predecessor = node -> left;
    26                 while (predecessor -> right && predecessor -> right != node)
    27                     predecessor = predecessor -> right;
    28                 if (!(predecessor -> right)) {
    29                     predecessor -> right = node;
    30                     node = node -> left;
    31                 }
    32                 else {
    33                     predecessor -> right = NULL;
    34                     nodes.push_back(node -> val);
    35                     node = node -> right;
    36                 }
    37             }
    38             else {
    39                 nodes.push_back(node -> val);
    40                 node = node -> right;
    41             }
    42         }
    43         return nodes;
    44     }
    45 };
  • 相关阅读:
    ASP.NET Web API 框架研究 Self Host模式下的消息处理管道
    ASP.NET Web API 框架研究 Web Host模式下的消息处理管道
    ASP.NET Web API 框架研究 核心的消息处理管道
    ASP.NET Web API 框架研究 Web Host模式路由及将请求转出到消息处理管道
    ASP.NET Web API 框架研究 ASP.NET Web API 路由
    ASP.NET Web API 框架研究 ASP.NET 路由
    ASP.NET Web API 入门 (API接口、寄宿方式、HttpClient调用)
    MVVM模式
    RESTful Web API 理解
    C# 函数式编程及Monads.net库
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4607756.html
Copyright © 2020-2023  润新知