您的位置:首页 > 编程语言 > C语言/C++

Binary Tree Postorder Traversal

2016-08-29 11:43 183 查看
一、问题描述

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 a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void postorder(TreeNode* node,vector<int> &res){
if(!node){
return ;
}else{
postorder(node -> left,res);
postorder(node -> right,res);
res.push_back(node -> val);
}
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> vec;
postorder(root,vec);
return vec;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ leetcode 遍历