-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Update 543-Diameter-of-Binary-Tree.py #482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The original `return -1` and `2+` actually cancel each other out and thus serve no purpose.
@MeViMo nice! What do you think about changing the res variable from a # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
res = 0
def dfs(root: Optional[TreeNode]):
nonlocal res
if not root:
return 0
left = dfs(root.left)
right = dfs(root.right)
res = max(res, left + right)
return 1 + max(left, right)
dfs(root)
return res |
@anandbaburajan That's probably what I would do to. You can push that change to this PR if you like. Up to the maintainers whether or not to include it. |
I can push the changes but I think you need to resolve the conflicts first. |
I don't need to but I did for your convenience. |
whitespace is annoying
Nowhere else in the repo are nested functions annotated so I guess we don't do it here either.
543-Diameter-of-Binary-Tree.py: Use nonlocal res instead of using a list
Thanks, @MeViMo and @anandbaburajan! |
The original
return -1
and2+
actually cancel each other out and thus serve no purpose, only causing confusion.