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 非递归实现竟然搞了半天,明天把四种遍历方法都写一遍,多复习几遍

你可能感兴趣的:(LeetCode)