0

I am practicing a leetcode question and I am having trouble updating my variable. I think I am not passing my reference correctly. I am expecting the answer to be 3 but I am getting 1. I ran through the code and the answer 3 is achieved but when i jump back out of my recursion I am getting 1.

The goal is the find the longest consecutive chain of nodes in a binary tree.

ex:

1
 \
  3
 / \
2   4
     \
      5

Answer would be 3 --> 3,4,5

Below is the runnable code:

class Node(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def DFS(self, root):
        count = 0
        if root:
            count += 1
            q = [root]
            while q:
                n = q.pop()
                T = 0
                if n.left:
                    if n.left.val == n.val + 1:
                        q.append(n.left)
                        T = 1
                if n.right:
                    if n.right.val == n.val + 1:
                        q.append(n.right)
                        T = 1
                if T:
                    count += 1
        return count

    def longestConsecutive(self, root, count=0):
        """
        :type root: TreeNode
        :rtype: int
        """
        c = count
        if root:
            c = max(c, self.DFS(root))
            self.longestConsecutive(root.left, c)
            self.longestConsecutive(root.right, c)
        return c

a = Node(1)
b = Node(3)
c = Node(2)
d = Node(4)
e = Node(5)

a.right = b
b.left = c; b.right = d
d.right = e

poop = Solution()
print(poop.longestConsecutive(a))
7
  • 4
    You will get better and faster answers if you produce a minimal example of the problem, without all the unrelated logic around it. If you're unsure about your DFS implementation, post that as a separate question Commented May 10, 2017 at 20:26
  • 4
    You can't pass variables by reference in Python. In fact, you never pass variables around at all; you can only pass objects. Commented May 10, 2017 at 20:28
  • 1
    Technically, Python does not support pass-by-reference semantics. Commented May 10, 2017 at 20:29
  • 1
    You're using T, an int variable, as a boolean flag. Just use True and False. Commented May 10, 2017 at 20:29
  • 1
    @user2357112 you assign objects to names Commented May 10, 2017 at 20:29

2 Answers 2

3

You're returning count from the longestConsecutive method but not assigning it to anything. Try this instead:

def longestConsecutive(self, root, count=0):
    """
    :type root: TreeNode
    :rtype: int
    """
    c = count
    if root:
        c = max(c, self.DFS(root))
        c = self.longestConsecutive(root.left, c)
        c = self.longestConsecutive(root.right, c)
    return c
Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is the the value of c is not saved. While inside of your recursive calls, the value of c is correctly set to 3. But when control flow begins to move back up the recursive stack, the value of c is lost. To fix this, you can make c an attribute of Solutions. That way, the value of c can be saved through out your recursive calls.

def longestConsecutive(self, root, count=0):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.c = count # make c an attribute of Solution.
        if root:
            self.c = max(self.c, self.DFS(root))
            self.longestConsecutive(root.left, self.c)
            self.longestConsecutive(root.right, self.c)
        return self.c

Which has the output:

3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.