leetcode刷题日记——Binary Tree Postorder Traversal

来源:互联网 发布:阿里云虚拟主机打不开 编辑:程序博客网 时间:2024/05/22 02:07
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{     vector<int> post; public:    vector<int> postorderTraversal(TreeNode* root) {         if(root==NULL) return post;         postorderTraversal(root->left);         postorderTraversal(root->right);         post.push_back(root->val);         return post;    }};


0 0
原创粉丝点击