LeetCode 543 Diameter of Binary Tree

LeetCode 543 Diameter of Binary Tree Problem

Download Code
# 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

        # number of nodes - 1 = length
        return self.ans - 1
Download Diameter of Binary Tree.py

List of all Diameter of Binary Tree problems

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.