mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
JavaScript版本的合并二叉树
This commit is contained in:
@ -370,6 +370,36 @@ func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
|
||||
```javascript
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* function TreeNode(val, left, right) {
|
||||
* this.val = (val===undefined ? 0 : val)
|
||||
* this.left = (left===undefined ? null : left)
|
||||
* this.right = (right===undefined ? null : right)
|
||||
* }
|
||||
*/
|
||||
/**
|
||||
* @param {TreeNode} root1
|
||||
* @param {TreeNode} root2
|
||||
* @return {TreeNode}
|
||||
*/
|
||||
var mergeTrees = function (root1, root2) {
|
||||
const preOrder = (root1, root2) => {
|
||||
if (!root1)
|
||||
return root2
|
||||
if (!root2)
|
||||
return root1;
|
||||
root1.val += root2.val;
|
||||
root1.left = preOrder(root1.left, root2.left);
|
||||
root1.right = preOrder(root1.right, root2.right);
|
||||
return root1;
|
||||
}
|
||||
return preOrder(root1, root2);
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user