• LeetCode_Binary Tree Inorder Traversal


    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> inorderTraversal(TreeNode *root) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            stack<TreeNode *> myStack;
            vector<int> result;
            if(root == NULL) return  result;
            TreeNode *current;
            current = root;
            while(current ||myStack.size()>0)
            {
               while(current){
                 myStack.push(current);
                 current = current->left;
               }
               
                if(myStack.size()>0)
                {
                   current = myStack.top();
                   myStack.pop();
                   result.push_back(current->val);
                   current = current->right;
                }
            }
            return result;
        }
    };

    惭愧,inorder traversal 非递归实现竟然搞了半天,明天把四种遍历方法都写一遍,多复习几遍

    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    linux top
    虚拟内存
    strcpy与strncpy
    C++ 踩内存
    MySQL -- 全文检索
    MySQL -- 全文检索(自然语言全文检索)
    MySQL -- innodb中cardinality是如何统计的
    MySQL -- Fast Index Creation
    python -- 生成器
    MySQL中模拟oracle中的rownum列
  • 原文地址:https://www.cnblogs.com/graph/p/3011438.html
Copyright © 2020-2023  润新知