【发布时间】:2021-04-17 21:43:51
【问题描述】:
我现在正在研究 DFS 方法来计算总和的路径。问题陈述是:
给定一棵二叉树和一个数字“S”,找到树中的所有路径,使得每条路径的所有节点值之和等于“S”。请注意,路径可以在任何节点开始或结束,但所有路径必须遵循从父节点到子节点(从上到下)的方向。
我的做法是:
def all_sum_path(root, target):
global count
count = 0
find_sum_path(root, target, [])
return count
def find_sum_path(root, target, allPath):
global count
if not root:
return 0
# add a space for current node
allPath.append(0)
# add current node values to all path
allPath = [i+root.value for i in allPath]
print(allPath)
# check if current path == target
for j in allPath:
if j == target:
count += 1
# recursive
find_sum_path(root.left, target, allPath)
find_sum_path(root.right, target, allPath)
# remove the current path
print('after', allPath)
allPath.pop()
print('after pop', allPath)
class TreeNode():
def __init__(self, _value):
self.value = _value
self.left, self.right, self.next = None, None, None
def main():
root = TreeNode(12)
root.left = TreeNode(7)
root.right = TreeNode(1)
root.left.left = TreeNode(4)
root.right.left = TreeNode(10)
root.right.right = TreeNode(5)
print(all_sum_path(root, 11))
main()
返回:
[1]
[8, 7]
[14, 13, 6]
after [14, 13, 6]
after pop [14, 13]
[13, 12, 5, 5]
after [13, 12, 5, 5]
after pop [13, 12, 5]
after [8, 7, 0, 0]
after pop [8, 7, 0]
[10, 9, 9]
[12, 11, 11, 2]
after [12, 11, 11, 2]
after pop [12, 11, 11]
[13, 12, 12, 3, 3]
after [13, 12, 12, 3, 3]
after pop [13, 12, 12, 3]
after [10, 9, 9, 0, 0]
after pop [10, 9, 9, 0]
after [1, 0, 0]
after pop [1, 0]
4
我认为问题在于我没有成功删除列表中最右边的节点。然后我更新了我的代码如下,我删除了allPath最右边的节点并创建了一个名为newAllPath的新列表来记录已经加上当前节点值的节点。
def all_sum_path(root, target):
global count
count = 0
find_sum_path(root, target, [])
return count
def find_sum_path(root, target, allPath):
global count
if not root:
return 0
# add a space for current node
allPath.append(0)
# add current node values to all path
newAllPath = [i+root.value for i in allPath]
print(allPath, newAllPath)
# check if current path == target
for j in newAllPath:
if j == target:
count += 1
# recursive
find_sum_path(root.left, target, newAllPath)
find_sum_path(root.right, target, newAllPath)
# remove the current path
print('after', allPath, newAllPath)
allPath.pop()
print('after pop', allPath, newAllPath)
class TreeNode():
def __init__(self, _value):
self.value = _value
self.left, self.right, self.next = None, None, None
def main():
root = TreeNode(1)
root.left = TreeNode(7)
root.right = TreeNode(9)
root.left.left = TreeNode(6)
root.left.right = TreeNode(5)
root.right.left = TreeNode(2)
root.right.right = TreeNode(3)
print(all_sum_path(root, 12))
root = TreeNode(12)
root.left = TreeNode(7)
root.right = TreeNode(1)
root.left.left = TreeNode(4)
root.right.left = TreeNode(10)
root.right.right = TreeNode(5)
print(all_sum_path(root, 11))
main()
返回:
[0] [1]
[1, 0] [8, 7]
[8, 7, 0] [14, 13, 6]
after [8, 7, 0] [14, 13, 6]
after pop [8, 7] [14, 13, 6]
[8, 7, 0] [13, 12, 5]
after [8, 7, 0] [13, 12, 5]
after pop [8, 7] [13, 12, 5]
after [1, 0] [8, 7]
after pop [1] [8, 7]
[1, 0] [10, 9]
[10, 9, 0] [12, 11, 2]
after [10, 9, 0] [12, 11, 2]
after pop [10, 9] [12, 11, 2]
[10, 9, 0] [13, 12, 3]
after [10, 9, 0] [13, 12, 3]
after pop [10, 9] [13, 12, 3]
after [1, 0] [10, 9]
after pop [1] [10, 9]
after [0] [1]
after pop [] [1]
3
我不确定为什么我无法在第一种方法中成功删除最正确的节点。但是,在我的第二种方法中,一旦我删除了allPath 中最右边的节点,它也会删除newAllPath 中的节点。
感谢您的帮助。我很困惑,一整天都被困在这里。
【问题讨论】:
标签: python recursion depth-first-search