简体   繁体   English

为什么这个二叉树前序遍历不返回

[英]Why is this binary tree preorder traversal returning none

Hi I am attempting to solve leetcode problem 1379 .嗨,我正在尝试解决leetcode问题1379

Here is the statement of the problem + link:这是问题的陈述+链接:

https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/ https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/

问题

Here is my failed attempt at a solution:这是我失败的解决方案尝试:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
        if cloned:
            if cloned.val == target.val:
                return cloned
            self.getTargetCopy(original, cloned.left, target)
            self.getTargetCopy(original, cloned.right, target)

I am failing to understand why this is not working.我不明白为什么这不起作用。

I think my solution is doing a preorder traversal of the tree and at each stage asking if the node in the cloned tree exists and if so is it's value equal to the value we are looking for?我认为我的解决方案是对树进行预序遍历,并在每个阶段询问克隆树中的节点是否存在,如果存在,它的值是否等于我们正在寻找的值? If it is then return that node and we have found the one we are looking for.如果是,则返回该节点,我们已经找到了我们正在寻找的节点。

If not then check simply carry on checking the tree.如果不是,则检查简单地继续检查树。 Since the values in the tree are guaranteed to be unique and the target value is guaranteed to be in the tree then and I am visiting every node then surely this must work at finding the answer.由于树中的值保证是唯一的,并且目标值保证在树中,并且我正在访问每个节点,因此这肯定可以找到答案。

However it is failing the automated tests, it doesn't even pass the example 1 (pictured above) and is saying it is returning null .然而,它没有通过自动化测试,它甚至没有通过示例 1(如上图所示),并表示它正在返回null

What am I missing?我错过了什么?

EDIT:编辑:

I tried printing my original solution with the arguements at each function call:我尝试在每个 function 电话中打印带有争论的原始解决方案:

__main__.Solution.getTargetCopy ( self = <__main__.Solution object at 0x000002B15CF31358>, original = 7, cloned = 7, target = 3 )
__main__.Solution.getTargetCopy ( self = <__main__.Solution object at 0x000002B15CF31358>, original = 7, cloned = 4, target = 3 )
__main__.Solution.getTargetCopy ( self = <__main__.Solution object at 0x000002B15CF31358>, original = 7, cloned = None, target = 3 )
__main__.Solution.getTargetCopy ( self = <__main__.Solution object at 0x000002B15CF31358>, original = 7, cloned = None, target = 3 )
__main__.Solution.getTargetCopy ( self = <__main__.Solution object at 0x000002B15CF31358>, original = 7, cloned = 3, target = 3 )
None

So as well can see the cloned tree traverses from 7 down to 4 then checks 4's left and right child which are both none, then it checks 7's right child 3 and confirms that it is the same as the target, at this point in the call cloned.val=3 so the return statement should return the node whose value is 3 which should be the right answer yet it seems to be returning none which is not even the value of cloned at that time (as proven by the print statement showing the arguments) what is going on?所以也可以看到克隆树从 7 向下遍历到 4 然后检查 4 的左右孩子都没有,然后它检查 7 的右孩子 3 并确认它与目标相同,此时在调用中cloned.val=3所以 return 语句应该返回值为 3 的节点,这应该是正确的答案,但它似乎返回 none,这甚至不是当时 cloned 的值(如打印语句所示)争论)发生了什么事?

When the if condition is not true, you don't return anything.if条件不为真时,您不会return任何内容。

So change these two lines:所以改变这两行:

self.getTargetCopy(original, cloned.left, target)
self.getTargetCopy(original, cloned.right, target)

To:到:

res = self.getTargetCopy(original, cloned.left, target)
if res:
    return res
return self.getTargetCopy(original, cloned.right, target)

Or even (using short circuit):甚至(使用短路):

return (self.getTargetCopy(original, cloned.left, target)
     or self.getTargetCopy(original, cloned.right, target))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM