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
return
二、思路
二叉树的后续遍历,非递归遍历稍微复杂一点,需要另外设置一个数据结构,改天补上,未完待续。
三、代码
/**
* 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;
}
};
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;
}
};
相关文章推荐
- [Leetcode] Binary Tree Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode145—Binary Tree Postorder Traversal
- [LeetCode][JavaScript]Binary Tree Postorder Traversal
- LeetCode Binary Tree Postorder Traversal
- 【LeetCode】Binary Tree Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Data Structure: How to traversal Binary Tree Iteratively (Preorder, Inorder and Postorder)
- Binary Tree Postorder Traversal
- LeetCode OJ Binary Tree Postorder Traversal
- [LeetCode]: 145: Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
- lintcode-medium-Construct Binary Tree from Inorder and Postorder Traversal