Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (54 sloc) 1.11 KB
/*
* @lc app=leetcode.cn id=145 lang=cpp
*
* [145] 二叉树的后序遍历
*
* https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/
*
* algorithms
* Hard (63.31%)
* Total Accepted: 10K
* Total Submissions: 15.8K
* Testcase Example: '[1,null,2,3]'
*
* 给定一个二叉树,返回它的 后序 遍历。
*
* 示例:
*
* 输入: [1,null,2,3]
* ⁠ 1
* ⁠ \
* ⁠ 2
* ⁠ /
* ⁠ 3
*
* 输出: [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:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ret;
postOrder(root,ret);
return ret;
}
void postOrder(TreeNode* root, vector<int> &ret)
{
if(!root) return ;
postOrder(root->left,ret);
postOrder(root->right,ret);
ret.push_back(root->val);
}
};
You can’t perform that action at this time.