您的位置:首页 > 其它

【leetcode】Binary Tree Postorder Traversal

2014-06-11 10:18 288 查看
Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree
{1,#,2,3}
,

1
\
2
/
3


return
[3,2,1]
.

题解:用了平凡的递归方法:

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> answer;
post(root,answer);
return answer;
}
void post(TreeNode* root,vector<int>& nodes){
if(root == NULL)
return;
post(root->left,nodes);
post(root->right,nodes);
nodes.push_back(root->val);
return;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: