代码改变世界

leetcode - Binary Tree Postorder Traversal

2013-11-10 10:46  张汉生  阅读(192)  评论(0编辑  收藏  举报

 

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     void postTra(TreeNode * node, vector<int>&rlt){
13         if (node==NULL)
14             return;
15         postTra(node->left, rlt);
16         postTra(node->right, rlt);
17         rlt.push_back(node->val);
18     }
19     vector<int> postorderTraversal(TreeNode *root) {
20         // IMPORTANT: Please reset any member data you declared, as
21         // the same Solution instance will be reused for each test case.
22         vector<int> rlt;
23         postTra(root,rlt);
24         return rlt;
25     }
26 };