Very clear , easy understand C++ Solution


  • 0
    F
    class Solution {
    public:
    	vector<int> postorderTraversal(TreeNode* root) {                                                                                                                          
    		vector<int> result; 
    		stack<TreeNode*> s;
    		if(root == NULL)return result;
    		s.push(root);
    		while(!s.empty())
    		{
    			TreeNode* t = s.top();
    			if(t->left == NULL && t->right == NULL)
    			{
    				result.push_back(t->val);
    				s.pop();
    			}
    			if(t->right != NULL)
    			{
    				s.push(t->right);
    				t->right = NULL;
    			}
    			if(t->left != NULL)
    			{
    				s.push(t->left);
    				t->left = NULL;
    			}
    		}
    		return result;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.