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 };

 

posted @ 2017-10-29 19:12  绿宝宝怪  阅读(176)  评论(0编辑  收藏  举报
#site_nav_under,#ad_under_post_holder,#under_post_news,#google_ad_c2,#under_post_kb{ width:0; height:0; display:none; overflow:hidden; }