diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/README.md b/solution/0500-0599/0543.Diameter of Binary Tree/README.md index f8359280524f3..4260bd07e8974 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/README.md +++ b/solution/0500-0599/0543.Diameter of Binary Tree/README.md @@ -56,7 +56,11 @@ tags: -### 方法一 +### 方法一:枚举 + DFS + +我们可以枚举二叉树的每个节点,以该节点为根节点,计算其左右子树的最大深度 $\textit{l}$ 和 $\textit{r}$,则该节点的直径为 $\textit{l} + \textit{r}$。取所有节点的直径的最大值即为二叉树的直径。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。 @@ -106,7 +110,6 @@ class Solution { private int ans; public int diameterOfBinaryTree(TreeNode root) { - ans = 0; dfs(root); return ans; } @@ -115,10 +118,10 @@ class Solution { if (root == null) { return 0; } - int left = dfs(root.left); - int right = dfs(root.right); - ans = Math.max(ans, left + right); - return 1 + Math.max(left, right); + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); } } ``` @@ -139,21 +142,20 @@ class Solution { */ class Solution { public: - int ans; - int diameterOfBinaryTree(TreeNode* root) { - ans = 0; + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left); + int r = dfs(root->right); + ans = max(ans, l + r); + return 1 + max(l, r); + }; dfs(root); return ans; } - - int dfs(TreeNode* root) { - if (!root) return 0; - int left = dfs(root->left); - int right = dfs(root->right); - ans = max(ans, left + right); - return 1 + max(left, right); - } }; ``` @@ -168,19 +170,18 @@ public: * Right *TreeNode * } */ -func diameterOfBinaryTree(root *TreeNode) int { - ans := 0 +func diameterOfBinaryTree(root *TreeNode) (ans int) { var dfs func(root *TreeNode) int dfs = func(root *TreeNode) int { if root == nil { return 0 } - left, right := dfs(root.Left), dfs(root.Right) - ans = max(ans, left+right) - return 1 + max(left, right) + l, r := dfs(root.Left), dfs(root.Right) + ans = max(ans, l+r) + return 1 + max(l, r) } dfs(root) - return ans + return } ``` @@ -202,19 +203,17 @@ func diameterOfBinaryTree(root *TreeNode) int { */ function diameterOfBinaryTree(root: TreeNode | null): number { - let res = 0; - const dfs = (root: TreeNode | null) => { - if (root == null) { + let ans = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { return 0; } - const { left, right } = root; - const l = dfs(left); - const r = dfs(right); - res = Math.max(res, l + r); - return Math.max(l, r) + 1; + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); }; dfs(root); - return res; + return ans; } ``` @@ -242,21 +241,90 @@ function diameterOfBinaryTree(root: TreeNode | null): number { use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root: &Option>>, res: &mut i32) -> i32 { - if root.is_none() { + pub fn diameter_of_binary_tree(root: Option>>) -> i32 { + let mut ans = 0; + fn dfs(root: Option>>, ans: &mut i32) -> i32 { + match root { + Some(node) => { + let node = node.borrow(); + let l = dfs(node.left.clone(), ans); + let r = dfs(node.right.clone(), ans); + + *ans = (*ans).max(l + r); + + 1 + l.max(r) + } + None => 0, + } + } + dfs(root, &mut ans); + ans + } +} +``` + +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var diameterOfBinaryTree = function (root) { + let ans = 0; + const dfs = root => { + if (!root) { return 0; } - let root = root.as_ref().unwrap().as_ref().borrow(); - let left = Self::dfs(&root.left, res); - let right = Self::dfs(&root.right, res); - *res = (*res).max(left + right); - left.max(right) + 1 + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); + }; + dfs(root); + return ans; +}; +``` + +#### C# + +```cs +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + private int ans; + + public int DiameterOfBinaryTree(TreeNode root) { + dfs(root); + return ans; } - pub fn diameter_of_binary_tree(root: Option>>) -> i32 { - let mut res = 0; - Self::dfs(&root, &mut res); - res + private int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.Max(ans, l + r); + return 1 + Math.Max(l, r); } } ``` @@ -272,23 +340,22 @@ impl Solution { * struct TreeNode *right; * }; */ - -#define max(a, b) (((a) > (b)) ? (a) : (b)) - -int dfs(struct TreeNode* root, int* res) { - if (!root) { +int dfs(struct TreeNode* root, int* ans) { + if (root == NULL) { return 0; } - int left = dfs(root->left, res); - int right = dfs(root->right, res); - *res = max(*res, left + right); - return max(left, right) + 1; + int l = dfs(root->left, ans); + int r = dfs(root->right, ans); + if (l + r > *ans) { + *ans = l + r; + } + return 1 + (l > r ? l : r); } int diameterOfBinaryTree(struct TreeNode* root) { - int res = 0; - dfs(root, &res); - return res; + int ans = 0; + dfs(root, &ans); + return ans; } ``` @@ -296,60 +363,4 @@ int diameterOfBinaryTree(struct TreeNode* root) { - - -### 方法二 - - - -#### Python3 - -```python -# 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: TreeNode) -> int: - def build(root): - if root is None: - return - nonlocal d - if root.left: - d[root].add(root.left) - d[root.left].add(root) - if root.right: - d[root].add(root.right) - d[root.right].add(root) - build(root.left) - build(root.right) - - def dfs(u, t): - nonlocal ans, vis, d, next - if u in vis: - return - vis.add(u) - if t > ans: - ans = t - next = u - for v in d[u]: - dfs(v, t + 1) - - d = defaultdict(set) - ans = 0 - next = root - build(root) - vis = set() - dfs(next, 0) - vis.clear() - dfs(next, 0) - return ans -``` - - - - - diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/README_EN.md b/solution/0500-0599/0543.Diameter of Binary Tree/README_EN.md index acc4e7107025f..74bce9b151e72 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/README_EN.md +++ b/solution/0500-0599/0543.Diameter of Binary Tree/README_EN.md @@ -54,7 +54,11 @@ tags: -### Solution 1 +### Solution 1: Enumeration + DFS + +We can enumerate each node of the binary tree, and for each node, calculate the maximum depth of its left and right subtrees, $\textit{l}$ and $\textit{r}$, respectively. The diameter of the node is $\textit{l} + \textit{r}$. The maximum diameter among all nodes is the diameter of the binary tree. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree. @@ -104,7 +108,6 @@ class Solution { private int ans; public int diameterOfBinaryTree(TreeNode root) { - ans = 0; dfs(root); return ans; } @@ -113,10 +116,10 @@ class Solution { if (root == null) { return 0; } - int left = dfs(root.left); - int right = dfs(root.right); - ans = Math.max(ans, left + right); - return 1 + Math.max(left, right); + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); } } ``` @@ -137,21 +140,20 @@ class Solution { */ class Solution { public: - int ans; - int diameterOfBinaryTree(TreeNode* root) { - ans = 0; + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left); + int r = dfs(root->right); + ans = max(ans, l + r); + return 1 + max(l, r); + }; dfs(root); return ans; } - - int dfs(TreeNode* root) { - if (!root) return 0; - int left = dfs(root->left); - int right = dfs(root->right); - ans = max(ans, left + right); - return 1 + max(left, right); - } }; ``` @@ -166,19 +168,18 @@ public: * Right *TreeNode * } */ -func diameterOfBinaryTree(root *TreeNode) int { - ans := 0 +func diameterOfBinaryTree(root *TreeNode) (ans int) { var dfs func(root *TreeNode) int dfs = func(root *TreeNode) int { if root == nil { return 0 } - left, right := dfs(root.Left), dfs(root.Right) - ans = max(ans, left+right) - return 1 + max(left, right) + l, r := dfs(root.Left), dfs(root.Right) + ans = max(ans, l+r) + return 1 + max(l, r) } dfs(root) - return ans + return } ``` @@ -200,19 +201,17 @@ func diameterOfBinaryTree(root *TreeNode) int { */ function diameterOfBinaryTree(root: TreeNode | null): number { - let res = 0; - const dfs = (root: TreeNode | null) => { - if (root == null) { + let ans = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { return 0; } - const { left, right } = root; - const l = dfs(left); - const r = dfs(right); - res = Math.max(res, l + r); - return Math.max(l, r) + 1; + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); }; dfs(root); - return res; + return ans; } ``` @@ -240,21 +239,90 @@ function diameterOfBinaryTree(root: TreeNode | null): number { use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root: &Option>>, res: &mut i32) -> i32 { - if root.is_none() { + pub fn diameter_of_binary_tree(root: Option>>) -> i32 { + let mut ans = 0; + fn dfs(root: Option>>, ans: &mut i32) -> i32 { + match root { + Some(node) => { + let node = node.borrow(); + let l = dfs(node.left.clone(), ans); + let r = dfs(node.right.clone(), ans); + + *ans = (*ans).max(l + r); + + 1 + l.max(r) + } + None => 0, + } + } + dfs(root, &mut ans); + ans + } +} +``` + +#### JavaScript + +```js +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var diameterOfBinaryTree = function (root) { + let ans = 0; + const dfs = root => { + if (!root) { return 0; } - let root = root.as_ref().unwrap().as_ref().borrow(); - let left = Self::dfs(&root.left, res); - let right = Self::dfs(&root.right, res); - *res = (*res).max(left + right); - left.max(right) + 1 + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); + }; + dfs(root); + return ans; +}; +``` + +#### C# + +```cs +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + private int ans; + + public int DiameterOfBinaryTree(TreeNode root) { + dfs(root); + return ans; } - pub fn diameter_of_binary_tree(root: Option>>) -> i32 { - let mut res = 0; - Self::dfs(&root, &mut res); - res + private int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.Max(ans, l + r); + return 1 + Math.Max(l, r); } } ``` @@ -270,23 +338,22 @@ impl Solution { * struct TreeNode *right; * }; */ - -#define max(a, b) (((a) > (b)) ? (a) : (b)) - -int dfs(struct TreeNode* root, int* res) { - if (!root) { +int dfs(struct TreeNode* root, int* ans) { + if (root == NULL) { return 0; } - int left = dfs(root->left, res); - int right = dfs(root->right, res); - *res = max(*res, left + right); - return max(left, right) + 1; + int l = dfs(root->left, ans); + int r = dfs(root->right, ans); + if (l + r > *ans) { + *ans = l + r; + } + return 1 + (l > r ? l : r); } int diameterOfBinaryTree(struct TreeNode* root) { - int res = 0; - dfs(root, &res); - return res; + int ans = 0; + dfs(root, &ans); + return ans; } ``` @@ -294,60 +361,4 @@ int diameterOfBinaryTree(struct TreeNode* root) { - - -### Solution 2 - - - -#### Python3 - -```python -# 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: TreeNode) -> int: - def build(root): - if root is None: - return - nonlocal d - if root.left: - d[root].add(root.left) - d[root.left].add(root) - if root.right: - d[root].add(root.right) - d[root.right].add(root) - build(root.left) - build(root.right) - - def dfs(u, t): - nonlocal ans, vis, d, next - if u in vis: - return - vis.add(u) - if t > ans: - ans = t - next = u - for v in d[u]: - dfs(v, t + 1) - - d = defaultdict(set) - ans = 0 - next = root - build(root) - vis = set() - dfs(next, 0) - vis.clear() - dfs(next, 0) - return ans -``` - - - - - diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.c b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.c index 03aaad1cedc4c..120361ad899e8 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.c +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.c @@ -6,21 +6,20 @@ * struct TreeNode *right; * }; */ - -#define max(a, b) (((a) > (b)) ? (a) : (b)) - -int dfs(struct TreeNode* root, int* res) { - if (!root) { +int dfs(struct TreeNode* root, int* ans) { + if (root == NULL) { return 0; } - int left = dfs(root->left, res); - int right = dfs(root->right, res); - *res = max(*res, left + right); - return max(left, right) + 1; + int l = dfs(root->left, ans); + int r = dfs(root->right, ans); + if (l + r > *ans) { + *ans = l + r; + } + return 1 + (l > r ? l : r); } int diameterOfBinaryTree(struct TreeNode* root) { - int res = 0; - dfs(root, &res); - return res; -} \ No newline at end of file + int ans = 0; + dfs(root, &ans); + return ans; +} diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp index e0a83d57f1736..bdea76b091b93 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp @@ -11,19 +11,18 @@ */ class Solution { public: - int ans; - int diameterOfBinaryTree(TreeNode* root) { - ans = 0; + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left); + int r = dfs(root->right); + ans = max(ans, l + r); + return 1 + max(l, r); + }; dfs(root); return ans; } - - int dfs(TreeNode* root) { - if (!root) return 0; - int left = dfs(root->left); - int right = dfs(root->right); - ans = max(ans, left + right); - return 1 + max(left, right); - } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cs b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cs new file mode 100644 index 0000000000000..fb573be51aeec --- /dev/null +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cs @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + private int ans; + + public int DiameterOfBinaryTree(TreeNode root) { + dfs(root); + return ans; + } + + private int dfs(TreeNode root) { + if (root == null) { + return 0; + } + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.Max(ans, l + r); + return 1 + Math.Max(l, r); + } +} diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.go b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.go index 6607e7059f3c2..9638e8b212b56 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.go +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.go @@ -6,17 +6,16 @@ * Right *TreeNode * } */ -func diameterOfBinaryTree(root *TreeNode) int { - ans := 0 +func diameterOfBinaryTree(root *TreeNode) (ans int) { var dfs func(root *TreeNode) int dfs = func(root *TreeNode) int { if root == nil { return 0 } - left, right := dfs(root.Left), dfs(root.Right) - ans = max(ans, left+right) - return 1 + max(left, right) + l, r := dfs(root.Left), dfs(root.Right) + ans = max(ans, l+r) + return 1 + max(l, r) } dfs(root) - return ans -} \ No newline at end of file + return +} diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.java b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.java index 8411b54bed4f7..586f9553acad1 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.java +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.java @@ -17,7 +17,6 @@ class Solution { private int ans; public int diameterOfBinaryTree(TreeNode root) { - ans = 0; dfs(root); return ans; } @@ -26,9 +25,9 @@ private int dfs(TreeNode root) { if (root == null) { return 0; } - int left = dfs(root.left); - int right = dfs(root.right); - ans = Math.max(ans, left + right); - return 1 + Math.max(left, right); + int l = dfs(root.left); + int r = dfs(root.right); + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.js b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.js new file mode 100644 index 0000000000000..1032e56773e47 --- /dev/null +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.js @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var diameterOfBinaryTree = function (root) { + let ans = 0; + const dfs = root => { + if (!root) { + return 0; + } + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); + }; + dfs(root); + return ans; +}; diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.py b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.py index d87fde7b67e5b..5cbdcb8d6ec2c 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.py +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.py @@ -5,14 +5,14 @@ # self.left = left # self.right = right class Solution: - def diameterOfBinaryTree(self, root: TreeNode) -> int: - def dfs(root): + def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: + def dfs(root: Optional[TreeNode]) -> int: if root is None: return 0 + l, r = dfs(root.left), dfs(root.right) nonlocal ans - left, right = dfs(root.left), dfs(root.right) - ans = max(ans, left + right) - return 1 + max(left, right) + ans = max(ans, l + r) + return 1 + max(l, r) ans = 0 dfs(root) diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.rs b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.rs index 3d336dc2bc21a..207337cef6e6b 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.rs +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.rs @@ -19,20 +19,23 @@ use std::cell::RefCell; use std::rc::Rc; impl Solution { - fn dfs(root: &Option>>, res: &mut i32) -> i32 { - if root.is_none() { - return 0; - } - let root = root.as_ref().unwrap().as_ref().borrow(); - let left = Self::dfs(&root.left, res); - let right = Self::dfs(&root.right, res); - *res = (*res).max(left + right); - left.max(right) + 1 - } - pub fn diameter_of_binary_tree(root: Option>>) -> i32 { - let mut res = 0; - Self::dfs(&root, &mut res); - res + let mut ans = 0; + fn dfs(root: Option>>, ans: &mut i32) -> i32 { + match root { + Some(node) => { + let node = node.borrow(); + let l = dfs(node.left.clone(), ans); + let r = dfs(node.right.clone(), ans); + + *ans = (*ans).max(l + r); + + 1 + l.max(r) + } + None => 0, + } + } + dfs(root, &mut ans); + ans } } diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.ts b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.ts index 525fc8e9e9aed..28f75e0bf9b30 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.ts +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.ts @@ -13,17 +13,15 @@ */ function diameterOfBinaryTree(root: TreeNode | null): number { - let res = 0; - const dfs = (root: TreeNode | null) => { - if (root == null) { + let ans = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { return 0; } - const { left, right } = root; - const l = dfs(left); - const r = dfs(right); - res = Math.max(res, l + r); - return Math.max(l, r) + 1; + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans = Math.max(ans, l + r); + return 1 + Math.max(l, r); }; dfs(root); - return res; + return ans; } diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution2.py b/solution/0500-0599/0543.Diameter of Binary Tree/Solution2.py deleted file mode 100644 index aa1f617d5fb0a..0000000000000 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution2.py +++ /dev/null @@ -1,41 +0,0 @@ -# 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: TreeNode) -> int: - def build(root): - if root is None: - return - nonlocal d - if root.left: - d[root].add(root.left) - d[root.left].add(root) - if root.right: - d[root].add(root.right) - d[root.right].add(root) - build(root.left) - build(root.right) - - def dfs(u, t): - nonlocal ans, vis, d, next - if u in vis: - return - vis.add(u) - if t > ans: - ans = t - next = u - for v in d[u]: - dfs(v, t + 1) - - d = defaultdict(set) - ans = 0 - next = root - build(root) - vis = set() - dfs(next, 0) - vis.clear() - dfs(next, 0) - return ans