我正在研究合并两个二叉树的问题(https://www.geeksforgeeks.org/merge-two-binary-trees-node-sum/)我很难理解一些递归。为什么要将递归语句设置为t1。左
和t1。对吧
?当你这样做的时候,t1。左
是否等于两个值?
我只是不确定为什么我们要将递归语句设置为t1.left或
t1.right'
class newNode:
def __init__(self, data):
self.data = data
self.left = self.right = None
def inorder(node):
if (not node):
return
inorder(node.left)
print(node.data, end = " ")
inorder(node.right)
def MergeTrees(t1, t2):
if (not t1):
return t2
if (not t2):
return t1
t1.data += t2.data
t1.left = MergeTrees(t1.left, t2.left)
t1.right = MergeTrees(t1.right, t2.right)
return t1
if __name__ == '__main__':
# Let us construct the first Binary Tree
# 1
# / \
# 2 3
# / \ \
# 4 5 6
root1 = newNode(1)
root1.left = newNode(2)
root1.right = newNode(3)
root1.left.left = newNode(4)
root1.left.right = newNode(5)
root1.right.right = newNode(6)
# Let us construct the second Binary Tree
# 4
# / \
# 1 7
# / / \
# 3 2 6
root2 = newNode(4)
root2.left = newNode(1)
root2.right = newNode(7)
root2.left.left = newNode(3)
root2.right.left = newNode(2)
root2.right.right = newNode(6)
root3 = MergeTrees(root1, root2)
print("The Merged Binary Tree is:")
inorder(root3)
首先,在构建节点时,可以设置
左
和右
分支-
class Node:
def __init__(self, data, left = None, right = None):
self.data = data
self.left = left
self.right = right
现在,我们可以直接构建它们,而不是使用
node.left=...
和node.right=...
来变异树-
# Let us construct the first Binary Tree
# 1
# / \
# 2 3
# / \ \
# 4 5 6
t1 = Node(1, Node(2, Node(4), Node(5)), Node(3, None, Node(6)))
在继续之前,让我们在
节点
上实现\uuuu str\uuu
,这样我们就可以可视化树了-
class Node:
def __init__(...):
# ...
def __str__(self, pre="", child=""):
if self is None:
return "()"
else:
return f"({self.data} {self.left} {self.right})"
print(t1)
# (1 (2 (4 None None) (5 None None)) (3 None (6 None None)))
现在让我们实现
merge
。由于能够在节点
构造函数中指定左
和右
值,因此编写此函数更容易-
def merge(t1, t2):
if t1 is None and t2 is None:
return None
elif t1 is None:
return t2
elif t2 is None:
return t1
else:
return Node(t1.data + t2.data, merge(t1.left, t2.left), merge(t1.right, t2.right)
print(merge(t1, t1))
# (2 (4 (8 None None) (10 None None)) (6 None (12 None None)))
现在我们可以看到
如何轻松地执行其他操作。将另一个参数添加到
merge
可以使用任何操作进行合并-
def merge(f, t1, t2):
if t1 is None and t2 is None:
return None
elif t1 is None:
return t2
elif t2 is None:
return t1
else:
return Node(
f(t1.data, t2.data),
merge(f, t1.left, t2.left),
merge(f, t1.right, t2.right)
)
print(merge(lambda a, b: a + b, t1, t1))
# (2 (4 (8 None None) (10 None None)) (6 None (12 None None)))
print(merge(lambda a, b: a * b, t1, t1))
# (1 (4 (16 None None) (25 None None)) (9 None (36 None None)))
或者使用
操作员
模块-
from operator import add, mul
print(merge(add, t1, t1))
# (2 (4 (8 None None) (10 None None)) (6 None (12 None None)))
print(merge(mul, t1, t1))
# (1 (4 (16 None None) (25 None None)) (9 None (36 None None)))
要使用递归合并树,请遵循典型公式:
在这种情况下,对其中一棵树进行合并非常方便。合并当前根节点。然后在左边的子对象上再次出现,它合并了t2。左
进入t1。左
;您将其分配给t1。左
,这样合并后的左子树将干净地替换原始子树。对正确的子树也要这样做。
清楚了吗?
问题是: 给定两棵二叉树,想象一下,当您将其中一棵树覆盖另一棵树时,两棵树的一些节点重叠,而其他节点则不重叠。 您需要将它们合并到一个新的二叉树中。合并规则是,如果两个节点重叠,则将节点值加起来作为合并节点的新值。否则,非空节点将用作新树的节点。 注意:合并过程必须从两棵树的根节点开始。 我试图解决这个leetcode问题,但总是得到错误的答案。 我的答案是: 似乎我丢失了节点4和节点7。 然而,
class Node(object): def __init__(self, data, left=None, right=None): self.data = data self.left = left self.right = right tree = Node(1, Node(3, Node(7, Node(0)), Node(6)), Node(2, Node
我正在尝试合并2棵二叉树,而不用担心使结果树平衡。这是我的解决方案,但不起作用。为什么Treenode ans和head从合并函数返回时设置为0。正如我所理解的,由于TreeNode不是原始类型,因此指向ans的head应该在调用合并函数后使用生成的树更新https://leetcode.com/problems/merge-two-binary-trees/
我被这个问题的修改版本困住了(在二叉树中找到距离为k的两个节点)。 我试图定义两个节点之间的距离,我认为这是沿着树状分支从节点n1移动到节点n2所需的最小节点数。 从这个假设出发,我得到了一个情况,我认为我需要知道每个节点是在根的左边还是右边。 案例1:如果n1和n2位于不同的一侧,那么我爬到根部(距离等于节点n1的深度-假设n1位于左侧),然后向下跑到右侧节点n2(距离等于节点n2的深度)。所以
我已经在“合并两棵二叉树”上工作了好几个小时了,我不明白为什么我的代码不起作用。树t1被指定为[1,3,2,5],树t2被指定为[2,1,3,null,4,null,7],我必须通过对重叠节点求和并尽可能避免null来合并这两棵树,因此结果应该是[3,4,5,5,4,null,7]。我不是像我应该的那样创建一棵新树,而是重写树t1以获得所需的结果。我的代码如下: 我的代码运行时没有错误,我的最终结
所以我想进入我的树(假设它没有重复项并且分支正确)并找到参数中给出的元素。我发现我的方法给了我一个 BinaryNode,它类似于我想要的(根)在其字段中,但实际上不是根。我没有覆盖等于方法。使用 equals 方法,当比较返回的对象和根时,测试返回 false。我想知道为什么我的变量 elementNode 在设置为 null 时不引用(因此更改)根为 null。 二进制节点是使用泛型实现的。调