LeetCode 之 Binary Tree Postorder Traversal

原题:Given a binary tree, return the postorder traversal of its nodes' values.

二叉树后序遍历,可以看看先序和中序

直接上代码(20ms):

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        vector<int>result;
        postOrder(root,result);
        return result;
    }
    
    void postOrder(TreeNode* root , vector<int>&result){
        if(!root) return;
        postOrder(root->left,result);
        postOrder(root->right,result);
        result.push_back(root->val);
    }
};


你可能感兴趣的:(LeetCode)