题目链接: Binary Tree PostOrder Trveral
题目意思: 给定一棵二叉树,求后续遍历序列
代码:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void getPostOrder (TreeNode *root, vector<int>& output); vector<int> postorderTraversal(TreeNode *root); }; void Solution::getPostOrder(TreeNode *root, vector<int>& output) { if (NULL == root) { return; } getPostOrder(root->left, output); getPostOrder(root->right, output); output.push_back(root->val); } vector<int> Solution::postorderTraversal(TreeNode *root) { vector<int> output; output.clear(); getPostOrder(root, output); return output; }
时间: 2024-10-22 03:56:56