MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / mergeTrees

Method mergeTrees

memo/treenode.cpp:24–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22class Solution {
23public:
24 TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
25 if(root1 == nullptr && root2 == nullptr) {
26 return nullptr;
27 } else if (root1 == nullptr) {
28 return root2;
29 } else if (root2 == nullptr) {
30 return root1;
31 } else {
32 TreeNode* root = new TreeNode(root1->val + root2->val);
33 root->left = mergeTrees(root1->left, root2->left);
34 root->right = mergeTrees(root1->right, root2->right);
35 return root;
36 }
37 }
38};
39
40

Callers 1

mainFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected