Leetcode 145.binary tree postord
2017-10-01 本文已影响0人
岛上痴汉
原题地址:https://leetcode.com/problems/binary-tree-postorder-traversal/description/
题目描述
Given a binary tree, return the postorder traversal of its nodes' values.
后续遍历二叉树
思路
递归写法和非递归写法。难度设置成Hard应该要写非递归的比较合理一些,不过暂时不会…先写个递归的凑数
代码
/**
* 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> result;
vector<int> postorderTraversal(TreeNode* root) {
if(root==NULL){
}else{
postorderTraversal(root->left);
postorderTraversal(root->right);
result.push_back(root->val);
}
return result;
}
};