|
| 1 | +## 题目地址 |
| 2 | +https://leetcode-cn.com/problems/merge-two-binary-trees/ |
| 3 | + |
| 4 | +## 思路 |
| 5 | + |
| 6 | +四种写法,总有一款适合你,其实这道题目迭代法实现是比较困难的,大家可以试一试,是一道不错的面试进阶题目。 |
| 7 | + |
| 8 | +四种写法如下: |
| 9 | + |
| 10 | +1. 递归修改了输入树的结构 |
| 11 | +2. 递归不修改树的结构 |
| 12 | +3. 递归,一波指针的操作,自己写的野路子(可以用来深度理解一下C++的指针) |
| 13 | +4. 迭代(这应该是最简单直观的迭代法代码了,一看就懂) |
| 14 | + |
| 15 | +## C++代码 |
| 16 | + |
| 17 | +### 递归 |
| 18 | + |
| 19 | +修改了输入树的结构 |
| 20 | +``` |
| 21 | +class Solution { |
| 22 | +public: |
| 23 | + TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { |
| 24 | + if (t1 == NULL) return t2; |
| 25 | + if (t2 == NULL) return t1; |
| 26 | + t1->val += t2->val; |
| 27 | + t1->left = mergeTrees(t1->left, t2->left); |
| 28 | + t1->right = mergeTrees(t1->right, t2->right); |
| 29 | + return t1; |
| 30 | + } |
| 31 | +}; |
| 32 | +``` |
| 33 | + |
| 34 | +不修改输入树的结构 |
| 35 | +``` |
| 36 | +class Solution { |
| 37 | +public: |
| 38 | + TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { |
| 39 | + if (t1 == NULL) return t2; |
| 40 | + if (t2 == NULL) return t1; |
| 41 | + TreeNode* root = new TreeNode(0); |
| 42 | + root->val = t1->val + t2->val; |
| 43 | + root->left = mergeTrees(t1->left, t2->left); |
| 44 | + root->right = mergeTrees(t1->right, t2->right); |
| 45 | + return root; |
| 46 | + } |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +一波指针的操作,自己写的野路子 |
| 51 | +想要更改二叉树的值,应该传入指向指针的指针, 如果process(t1, t2);这么写的话,其实只是传入的一个int型的指针,并没有传入地址,要传入指向指针的指针才能完成对t1的修改。 |
| 52 | +``` |
| 53 | +class Solution { |
| 54 | +public: |
| 55 | + void process(TreeNode** t1, TreeNode** t2) { |
| 56 | + if ((*t1) == NULL && (*t2) == NULL) return; |
| 57 | + if ((*t1) != NULL && (*t2) != NULL) { |
| 58 | + (*t1)->val += (*t2)->val; |
| 59 | + } |
| 60 | + if ((*t1) == NULL && (*t2) != NULL) { |
| 61 | + *t1 = *t2; |
| 62 | + return; |
| 63 | + } |
| 64 | + if ((*t1) != NULL && (*t2) == NULL) { |
| 65 | + return; |
| 66 | + } |
| 67 | + process(&((*t1)->left), &((*t2)->left)); |
| 68 | + process(&((*t1)->right), &((*t2)->right)); |
| 69 | + } |
| 70 | + TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { |
| 71 | + process(&t1, &t2); |
| 72 | + return t1; |
| 73 | + } |
| 74 | +}; |
| 75 | +``` |
| 76 | +### 迭代 |
| 77 | + |
| 78 | +这应该是最简单直观的迭代法了 |
| 79 | +``` |
| 80 | +class Solution { |
| 81 | +public: |
| 82 | + TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { |
| 83 | + if (t1 == NULL) return t2; |
| 84 | + if (t2 == NULL) return t1; |
| 85 | + queue<TreeNode*> que; |
| 86 | + que.push(t1); |
| 87 | + que.push(t2); |
| 88 | + while(!que.empty()) { |
| 89 | + TreeNode* node1 = que.front(); que.pop(); |
| 90 | + TreeNode* node2 = que.front(); que.pop(); |
| 91 | + // 两个节点不为空,val相加 |
| 92 | + if (node1 != NULL && node2 != NULL) { |
| 93 | + node1->val += node2->val; |
| 94 | + } |
| 95 | + // 如果左节点都不为空,加入队列 |
| 96 | + if (node1->left != NULL && node2->left != NULL) { |
| 97 | + que.push(node1->left); |
| 98 | + que.push(node2->left); |
| 99 | + } |
| 100 | + // 如果右节点都不为空,加入队列 |
| 101 | + if (node1->right != NULL && node2->right != NULL) { |
| 102 | + que.push(node1->right); |
| 103 | + que.push(node2->right); |
| 104 | + } |
| 105 | + // 当t1的左节点 为空 t2左节点不为空,就赋值过去 |
| 106 | + if (node1->left == NULL && node2->left != NULL) { |
| 107 | + node1->left = node2->left; |
| 108 | + } |
| 109 | + // 当t1的右节点 为空 t2右节点不为空,就赋值过去 |
| 110 | + if (node1->right == NULL && node2->right != NULL) { |
| 111 | + node1->right = node2->right; |
| 112 | + } |
| 113 | + } |
| 114 | + return t1; |
| 115 | + } |
| 116 | +}; |
| 117 | +``` |
| 118 | + |
| 119 | +> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 |
0 commit comments