合并二叉树,叶子叠加合并
创建新的root结点,一个一个的将两颗二叉树的结点叠加起来
1 class Solution { 2 public: 3 TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { 4 if(!t1&&!t2){ 5 return 0; 6 } 7 TreeNode *root=new TreeNode(0); 8 if(t1) 9 root->val+=t1->val; 10 if(t2) 11 root->val+=t2->val; 12 root->left=mergeTrees(t1?t1->left:0,t2?t2->left:0); 13 root->right=mergeTrees(t1?t1->right:0,t2?t2->right:0); 14 return root; 15 } 16 };