mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
update 0617.合并二叉树: 优化 go 代码
This commit is contained in:
@ -46,7 +46,7 @@
|
|||||||
|
|
||||||
1. **确定递归函数的参数和返回值:**
|
1. **确定递归函数的参数和返回值:**
|
||||||
|
|
||||||
首先那么要合入两个二叉树,那么参数至少是要传入两个二叉树的根节点,返回值就是合并之后二叉树的根节点。
|
首先要合入两个二叉树,那么参数至少是要传入两个二叉树的根节点,返回值就是合并之后二叉树的根节点。
|
||||||
|
|
||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
|
|||||||
|
|
||||||
2. **确定终止条件:**
|
2. **确定终止条件:**
|
||||||
|
|
||||||
因为是传入了两个树,那么就有两个树遍历的节点t1 和 t2,如果t1 == NULL 了,两个树合并就应该是 t2 了啊(如果t2也为NULL也无所谓,合并之后就是NULL)。
|
因为是传入了两个树,那么就有两个树遍历的节点t1 和 t2,如果t1 == NULL 了,两个树合并就应该是 t2 了(如果t2也为NULL也无所谓,合并之后就是NULL)。
|
||||||
|
|
||||||
反过来如果t2 == NULL,那么两个数合并就是t1(如果t1也为NULL也无所谓,合并之后就是NULL)。
|
反过来如果t2 == NULL,那么两个数合并就是t1(如果t1也为NULL也无所谓,合并之后就是NULL)。
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ if (t2 == NULL) return t1; // 如果t2为空,合并之后就应该是t1
|
|||||||
|
|
||||||
3. **确定单层递归的逻辑:**
|
3. **确定单层递归的逻辑:**
|
||||||
|
|
||||||
单层递归的逻辑就比较好些了,这里我们用重复利用一下t1这个树,t1就是合并之后树的根节点(就是修改了原来树的结构)。
|
单层递归的逻辑就比较好写了,这里我们重复利用一下t1这个树,t1就是合并之后树的根节点(就是修改了原来树的结构)。
|
||||||
|
|
||||||
那么单层递归中,就要把两棵树的元素加到一起。
|
那么单层递归中,就要把两棵树的元素加到一起。
|
||||||
```
|
```
|
||||||
@ -144,7 +144,7 @@ public:
|
|||||||
|
|
||||||
**但是前序遍历是最好理解的,我建议大家用前序遍历来做就OK。**
|
**但是前序遍历是最好理解的,我建议大家用前序遍历来做就OK。**
|
||||||
|
|
||||||
如上的方法修改了t1的结构,当然也可以不修改t1和t2的结构,重新定一个树。
|
如上的方法修改了t1的结构,当然也可以不修改t1和t2的结构,重新定义一个树。
|
||||||
|
|
||||||
不修改输入树的结构,前序遍历,代码如下:
|
不修改输入树的结构,前序遍历,代码如下:
|
||||||
|
|
||||||
@ -214,7 +214,7 @@ public:
|
|||||||
|
|
||||||
## 拓展
|
## 拓展
|
||||||
|
|
||||||
当然也可以秀一波指针的操作,这是我写的野路子,大家就随便看看就行了,以防带跑遍了。
|
当然也可以秀一波指针的操作,这是我写的野路子,大家就随便看看就行了,以防带跑偏了。
|
||||||
|
|
||||||
如下代码中,想要更改二叉树的值,应该传入指向指针的指针。
|
如下代码中,想要更改二叉树的值,应该传入指向指针的指针。
|
||||||
|
|
||||||
@ -252,7 +252,7 @@ public:
|
|||||||
|
|
||||||
迭代法中,一般一起操作两个树都是使用队列模拟类似层序遍历,同时处理两个树的节点,这种方式最好理解,如果用模拟递归的思路的话,要复杂一些。
|
迭代法中,一般一起操作两个树都是使用队列模拟类似层序遍历,同时处理两个树的节点,这种方式最好理解,如果用模拟递归的思路的话,要复杂一些。
|
||||||
|
|
||||||
最后拓展中,我给了一个操作指针的野路子,大家随便看看就行了,如果学习C++的话,可以在去研究研究。
|
最后拓展中,我给了一个操作指针的野路子,大家随便看看就行了,如果学习C++的话,可以再去研究研究。
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
@ -417,43 +417,7 @@ class Solution:
|
|||||||
### Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
/**
|
// 前序遍历
|
||||||
* Definition for a binary tree node.
|
|
||||||
* type TreeNode struct {
|
|
||||||
* Val int
|
|
||||||
* Left *TreeNode
|
|
||||||
* Right *TreeNode
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
//前序遍历(递归遍历,跟105 106差不多的思路)
|
|
||||||
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
|
|
||||||
var value int
|
|
||||||
var nullNode *TreeNode//空node,便于遍历
|
|
||||||
nullNode=&TreeNode{
|
|
||||||
Val:0,
|
|
||||||
Left:nil,
|
|
||||||
Right:nil}
|
|
||||||
switch {
|
|
||||||
case t1==nil&&t2==nil: return nil//终止条件
|
|
||||||
default : //如果其中一个节点为空,则将该节点置为nullNode,方便下次遍历
|
|
||||||
if t1==nil{
|
|
||||||
value=t2.Val
|
|
||||||
t1=nullNode
|
|
||||||
}else if t2==nil{
|
|
||||||
value=t1.Val
|
|
||||||
t2=nullNode
|
|
||||||
}else {
|
|
||||||
value=t1.Val+t2.Val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
root:=&TreeNode{//构造新的二叉树
|
|
||||||
Val: value,
|
|
||||||
Left: mergeTrees(t1.Left,t2.Left),
|
|
||||||
Right: mergeTrees(t1.Right,t2.Right)}
|
|
||||||
return root
|
|
||||||
}
|
|
||||||
|
|
||||||
// 前序遍历简洁版
|
|
||||||
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
||||||
if root1 == nil {
|
if root1 == nil {
|
||||||
return root2
|
return root2
|
||||||
@ -479,28 +443,28 @@ func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
|
|||||||
queue = append(queue,root1)
|
queue = append(queue,root1)
|
||||||
queue = append(queue,root2)
|
queue = append(queue,root2)
|
||||||
|
|
||||||
for size:=len(queue);size>0;size=len(queue){
|
for size := len(queue); size>0; size=len(queue) {
|
||||||
node1 := queue[0]
|
node1 := queue[0]
|
||||||
queue = queue[1:]
|
queue = queue[1:]
|
||||||
node2 := queue[0]
|
node2 := queue[0]
|
||||||
queue = queue[1:]
|
queue = queue[1:]
|
||||||
node1.Val += node2.Val
|
node1.Val += node2.Val
|
||||||
// 左子树都不为空
|
// 左子树都不为空
|
||||||
if node1.Left != nil && node2.Left != nil{
|
if node1.Left != nil && node2.Left != nil {
|
||||||
queue = append(queue,node1.Left)
|
queue = append(queue,node1.Left)
|
||||||
queue = append(queue,node2.Left)
|
queue = append(queue,node2.Left)
|
||||||
}
|
}
|
||||||
// 右子树都不为空
|
// 右子树都不为空
|
||||||
if node1.Right !=nil && node2.Right !=nil{
|
if node1.Right !=nil && node2.Right !=nil {
|
||||||
queue = append(queue,node1.Right)
|
queue = append(queue, node1.Right)
|
||||||
queue = append(queue,node2.Right)
|
queue = append(queue, node2.Right)
|
||||||
}
|
}
|
||||||
// 树 1 的左子树为 nil,直接接上树 2 的左子树
|
// 树 1 的左子树为 nil,直接接上树 2 的左子树
|
||||||
if node1.Left == nil{
|
if node1.Left == nil {
|
||||||
node1.Left = node2.Left
|
node1.Left = node2.Left
|
||||||
}
|
}
|
||||||
// 树 1 的右子树为 nil,直接接上树 2 的右子树
|
// 树 1 的右子树为 nil,直接接上树 2 的右子树
|
||||||
if node1.Right == nil{
|
if node1.Right == nil {
|
||||||
node1.Right = node2.Right
|
node1.Right = node2.Right
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user