617. Merge Two Binary Trees 合并两个二叉树

Posted lvbbg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了617. Merge Two Binary Trees 合并两个二叉树相关的知识,希望对你有一定的参考价值。

617. Merge Two Binary Trees


 

递归!递归!

 1 class Solution {
 2 public:
 3     TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
 4         if ( t1 && t2 ) {
 5             TreeNode * root = new TreeNode(t1->val + t2->val);
 6             root->left = mergeTrees(t1->left, t2->left);
 7             root->right = mergeTrees(t1->right, t2->right);
 8             return root;
 9         } else {
10             return t1 ? t1 : t2;
11         }
12     }
13 };

 

以上是关于617. Merge Two Binary Trees 合并两个二叉树的主要内容,如果未能解决你的问题,请参考以下文章

617. Merge Two Binary Trees

617. Merge Two Binary Trees

617. Merge Two Binary Trees

[Leetcode] Binary tree -- 617. Merge Two Binary Trees

[leetcode-617-Merge Two Binary Trees]

LeetCode #617 Merge Two Binary Trees