lintcode 二叉树后序遍历

时间:2022-03-09 22:44:15
 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ //递归
class Solution {
public:
/*
* @param root: A Tree
* @return: Postorder in ArrayList which contains node values.
*/ void inorder(TreeNode *root, vector<int> &result) {
if (root->left != NULL) {
inorder(root->left, result);
}
if (root->right != NULL) {
inorder(root->right, result);
}
result.push_back(root->val);
} vector<int> postorderTraversal(TreeNode * root) {
// write your code here
vector<int> result;
if (root == NULL) {
return result;
}
inorder(root, result);
return result;
}
};