mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
增加 0617.合并二叉树 go版 (增加前序遍历简洁版解题)
增加前序遍历简洁版解题
This commit is contained in:
@ -368,6 +368,20 @@ func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
|
|||||||
Right: mergeTrees(t1.Right,t2.Right)}
|
Right: mergeTrees(t1.Right,t2.Right)}
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 前序遍历简洁版
|
||||||
|
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
||||||
|
if root1 == nil {
|
||||||
|
return root2
|
||||||
|
}
|
||||||
|
if root2 == nil {
|
||||||
|
return root1
|
||||||
|
}
|
||||||
|
root1.Val += root2.Val
|
||||||
|
root1.Left = mergeTrees(root1.Left, root2.Left)
|
||||||
|
root1.Right = mergeTrees(root1.Right, root2.Right)
|
||||||
|
return root1
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
Reference in New Issue
Block a user