C++ solution, 4ms, recursive


  • 0
    X
    class Solution
    {
    	public:
    		vector<int> postorderTraversal(TreeNode* root)
    		{
    			vector<int> res;
    			traverse(root, res);
    			return res;
    		}
    
    		void traverse(TreeNode* node, vector<int>& res)
    		{
    			if(NULL == node) return;
    			traverse(node->left, res);
    			traverse(node->right, res);
    			res.push_back(node->val);
    		}
    };

Log in to reply
 

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