文章目录
- 一、题目
- 二、解法
- 三、完整代码
所有的LeetCode题解索引,可以看这篇文章——【算法和数据结构】LeetCode题解。
一、题目
二、解法
思路分析:采用递归的方式遍历二叉树,【算法与数据结构】144、94、145LeetCode二叉树的前中后遍历(递归法、迭代法),递归法程序可以参考这篇文章。递归重要的是三步骤:输入参数和返回值;终止条件;单层递归逻辑。
程序如下:
class Solution {
public:
//1、 输入参数root1 root2
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
// 2、终止条件
if (!root1) return root2;
if (!root2) return root1;
// 3、单层递归逻辑
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
// 1、返回值 root1
return root1;
}
};
三、完整代码
# include <iostream>
# include <vector>
# include <string>
# include <queue>
# include <stack>
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:
//1、 输入参数root1 root2
TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
// 2、终止条件
if (!root1) return root2;
if (!root2) return root1;
// 3、单层递归逻辑
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
// 1、返回值 root1
return root1;
}
};
// 前序遍历,统一代码风格迭代写法
class Solution8 {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if (root != NULL) st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
if (node != NULL) {
st.pop();
if (node->right) st.push(node->right); // 右
if (node->left) st.push(node->left); // 左
st.push(node); // 中
st.push(NULL);
}
else {
st.pop();
node = st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
// 前序遍历迭代法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>&t, TreeNode * &node) {
if (!t.size() || t[0] == "NULL") return; // 退出条件
else {
node = new TreeNode(stoi(t[0].c_str())); // 中
if (t.size()) {
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->left); // 左
}
if (t.size()) {
t.assign(t.begin() + 1, t.end());
Tree_Generator(t, node->right); // 右
}
}
}
template<typename T>
void my_print(T& v, const string msg)
{
cout << msg << endl;
for (class T::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << ' ';
}
cout << endl;
}
template<class T1, class T2>
void my_print2(T1& v, const string str) {
cout << str << endl;
for (class T1::iterator vit = v.begin(); vit < v.end(); ++vit) {
for (class T2::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
}
// 层序遍历
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty()) {
int size = que.size(); // size必须固定, que.size()是不断变化的
vector<int> vec;
for (int i = 0; i < size; ++i) {
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
int main()
{
vector<string> t1 = { "1", "3", "5", "NULL", "NULL", "NULL", "2", "NULL", "NULL"}; // 前序遍历
my_print(t1, "目标树");
TreeNode* root1 = new TreeNode();
Tree_Generator(t1, root1);
vector<vector<int>> tree1 = levelOrder(root1);
my_print2<vector<vector<int>>, vector<int>>(tree1, "目标树:");
vector<string> t2 = { "2", "1", "NULL", "4", "NULL", "NULL", "3", "NULL", "7", "NULL", "NULL" }; // 前序遍历
my_print(t2, "目标树");
TreeNode* root2 = new TreeNode();
Tree_Generator(t2, root2);
vector<vector<int>> tree2 = levelOrder(root2);
my_print2<vector<vector<int>>, vector<int>>(tree2, "目标树:");
Solution s;
TreeNode* root = s.mergeTrees(root1, root2);
vector<vector<int>> tree = levelOrder(root);
my_print2<vector<vector<int>>, vector<int>>(tree, "目标树:");
system("pause");
return 0;
}
end