class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def longestUnivaluePath(self, root):
if root == None: return 0
def longestHelper(self, root):
# this function returns both the value and whether include_root = True
if root == None:
val = 0
include_root = False
return [val, include_root]
elif (root.left == None and root.right == None):
# leaf node
val = 1
include_root = True
return [val, include_root]
else:
if root.val == root.left.val: # current node val = left child val
if longestHelper(root.left)[1] == True:
lmax = longestHelper(root.left)[0] + 1
linclude = True
else:
lmax = longestHelper(root.left)[0]
linclude = False
else:
2 Responses
- forget "self"
- Line 39: TypeError: 'NoneType' object is not subscriptable -- when return is not [lmax, linclude]?
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.