Download Diameter of Binary Tree.py
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): # https://leetcode.com/problems/diameter-of-binary-tree/solution/ def diameterOfBinaryTree(self, root): self.ans = 1 def depth(node): if not node: return 0 L = depth(node.left) R = depth(node.right) self.ans = max(self.ans, L+R+1) return max(L, R) + 1 depth(root) # number of nodes - 1 = length return self.ans - 1
Leetcode 543 Diameter of Binary Tree problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.
self.val = x
Feedback is the most important part of any website.
If you have any query, suggestion or feedback, Please feel free to contact us.