所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
思路分析:这道题比较简单,不多说了,大家直接看代码就行。注意前中后遍历是指中间节点的遍历顺序。同时中序和后序的代码也很类似,这里给出三道题代码。
前序遍历程序如下:
class Solution {
public:
// 前序遍历
void traversal_preOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val); // 中
traversal_preOrder(cur->left, vec); // 左
traversal_preOrder(cur->right, vec); // 右
}
vector<int> preorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_preOrder(root, result);
return result;
}
};
复杂度分析可以参考这篇文章二叉树多种遍历的时间复杂度和空间复杂度。
中序遍历程序如下:
// 中序遍历
class Solution2 {
public:
void traversal_midOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_midOrder(cur->left, vec); // 左
vec.push_back(cur->val); // 中
traversal_midOrder(cur->right, vec); // 右
}
vector<int> inorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_midOrder(root, result);
return result;
}
};
后序遍历程序如下:
// 后序遍历
class Solution3 {
public:
void traversal_postOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_postOrder(cur->left, vec); // 左
traversal_postOrder(cur->right, vec); // 右
vec.push_back(cur->val); // 中
}
vector<int> postorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_postOrder(root, result);
return result;
}
};
# include
# include
using namespace std;
// 树节点定义
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// 前序遍历
class Solution {
public:
void traversal_preOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
vec.push_back(cur->val); // 中
traversal_preOrder(cur->left, vec); // 左
traversal_preOrder(cur->right, vec); // 右
}
vector<int> preorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_preOrder(root, result);
return result;
}
};
// 中序遍历
class Solution2 {
public:
void traversal_midOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_midOrder(cur->left, vec); // 左
vec.push_back(cur->val); // 中
traversal_midOrder(cur->right, vec); // 右
}
vector<int> inorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_midOrder(root, result);
return result;
}
};
// 后序遍历
class Solution3 {
public:
void traversal_postOrder(TreeNode* cur, vector<int>& vec) {
if (cur == NULL) return;
traversal_postOrder(cur->left, vec); // 左
traversal_postOrder(cur->right, vec); // 右
vec.push_back(cur->val); // 中
}
vector<int> postorderTraversal(TreeNode* root) {
if (root == NULL) return {};
if (root->left == NULL && root->right == NULL) return { root->val };
vector<int> result;
traversal_postOrder(root, result);
return result;
}
};
void my_print(vector <int>& v, string msg)
{
cout << msg << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
// 构造树
TreeNode* node3 = new TreeNode(3);
TreeNode* node2 = new TreeNode(2, node3, NULL);
TreeNode* root = new TreeNode(1, NULL, node2);
Solution s1;
vector<int> result = s1.preorderTraversal(root);
my_print(result, "前序遍历结果:");
system("pause");
return 0;
}
end