Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1963 from laitooo/0617-merge-two-binar…
Browse files Browse the repository at this point in the history
…y-trees.dart

Create: 0617-Merge-Two-Binary-Trees.dart
  • Loading branch information
tahsintunan authored Jan 9, 2023
2 parents e7534ad + a48bd42 commit 658a556
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dart/0617-merge-two-binary-trees.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* int val;
* TreeNode? left;
* TreeNode? right;
* TreeNode([this.val = 0, this.left, this.right]);
* }
*/
class Solution {
TreeNode? mergeTrees(TreeNode? root1, TreeNode? root2) {
if (root1 == null && root2 == null) {
return null;
}

int val1 = (root1 == null) ? 0 : root1.val;
int val2 = (root2 == null) ? 0 : root2.val;
TreeNode res = TreeNode();
res.val = val1 + val2;
res.left = mergeTrees((root1 == null) ? null : root1.left, (root2 == null) ? null : root2.left);
res.right = mergeTrees((root1 == null) ? null : root1.right, (root2 == null) ? null : root2.right);
return res;
}
}

0 comments on commit 658a556

Please sign in to comment.