GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/543_Diameter_of_Binary_Tree.py at master · qiyuangong/leetcode Expand file tree/ Copy path543_Diameter_of_Binary_Tree.pyMore file actions 21 lines (19 loc) · 619 Bytes / Copy path543_Diameter_of_Binary_Tree.pyFile metadata and controls21 lines (19 loc) · 619 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 # 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 |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |