Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def dia(root,ans):
- if root==None:
- return 0
- l=dia(root.left,ans)
- r=dia(root.right,ans)
- ans[0]=max(ans[0],l+r+1)
- return max(l,r)+1
- class Solution:
- def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
- ans=[-1]
- dia(root,ans)
- return ans[0]-1. #No. of nodes in terms of edges, not nodes so reduced 1
Advertisement
Add Comment
Please, Sign In to add comment