Skip to content

Commit acbf443

Browse files
authored
feat: add solutions to lc problem: No.0543 (#3911)
No.0543.Diameter of Binary Tree
1 parent 110b584 commit acbf443

File tree

12 files changed

+363
-329
lines changed

12 files changed

+363
-329
lines changed

solution/0500-0599/0543.Diameter of Binary Tree/README.md

+122-111
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一
59+
### 方法一:枚举 + DFS
60+
61+
我们可以枚举二叉树的每个节点,以该节点为根节点,计算其左右子树的最大深度 $\textit{l}$ 和 $\textit{r}$,则该节点的直径为 $\textit{l} + \textit{r}$。取所有节点的直径的最大值即为二叉树的直径。
62+
63+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
6064

6165
<!-- tabs:start -->
6266

@@ -106,7 +110,6 @@ class Solution {
106110
private int ans;
107111

108112
public int diameterOfBinaryTree(TreeNode root) {
109-
ans = 0;
110113
dfs(root);
111114
return ans;
112115
}
@@ -115,10 +118,10 @@ class Solution {
115118
if (root == null) {
116119
return 0;
117120
}
118-
int left = dfs(root.left);
119-
int right = dfs(root.right);
120-
ans = Math.max(ans, left + right);
121-
return 1 + Math.max(left, right);
121+
int l = dfs(root.left);
122+
int r = dfs(root.right);
123+
ans = Math.max(ans, l + r);
124+
return 1 + Math.max(l, r);
122125
}
123126
}
124127
```
@@ -139,21 +142,20 @@ class Solution {
139142
*/
140143
class Solution {
141144
public:
142-
int ans;
143-
144145
int diameterOfBinaryTree(TreeNode* root) {
145-
ans = 0;
146+
int ans = 0;
147+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> int {
148+
if (!root) {
149+
return 0;
150+
}
151+
int l = dfs(root->left);
152+
int r = dfs(root->right);
153+
ans = max(ans, l + r);
154+
return 1 + max(l, r);
155+
};
146156
dfs(root);
147157
return ans;
148158
}
149-
150-
int dfs(TreeNode* root) {
151-
if (!root) return 0;
152-
int left = dfs(root->left);
153-
int right = dfs(root->right);
154-
ans = max(ans, left + right);
155-
return 1 + max(left, right);
156-
}
157159
};
158160
```
159161
@@ -168,19 +170,18 @@ public:
168170
* Right *TreeNode
169171
* }
170172
*/
171-
func diameterOfBinaryTree(root *TreeNode) int {
172-
ans := 0
173+
func diameterOfBinaryTree(root *TreeNode) (ans int) {
173174
var dfs func(root *TreeNode) int
174175
dfs = func(root *TreeNode) int {
175176
if root == nil {
176177
return 0
177178
}
178-
left, right := dfs(root.Left), dfs(root.Right)
179-
ans = max(ans, left+right)
180-
return 1 + max(left, right)
179+
l, r := dfs(root.Left), dfs(root.Right)
180+
ans = max(ans, l+r)
181+
return 1 + max(l, r)
181182
}
182183
dfs(root)
183-
return ans
184+
return
184185
}
185186
```
186187

@@ -202,19 +203,17 @@ func diameterOfBinaryTree(root *TreeNode) int {
202203
*/
203204

204205
function diameterOfBinaryTree(root: TreeNode | null): number {
205-
let res = 0;
206-
const dfs = (root: TreeNode | null) => {
207-
if (root == null) {
206+
let ans = 0;
207+
const dfs = (root: TreeNode | null): number => {
208+
if (!root) {
208209
return 0;
209210
}
210-
const { left, right } = root;
211-
const l = dfs(left);
212-
const r = dfs(right);
213-
res = Math.max(res, l + r);
214-
return Math.max(l, r) + 1;
211+
const [l, r] = [dfs(root.left), dfs(root.right)];
212+
ans = Math.max(ans, l + r);
213+
return 1 + Math.max(l, r);
215214
};
216215
dfs(root);
217-
return res;
216+
return ans;
218217
}
219218
```
220219

@@ -242,21 +241,90 @@ function diameterOfBinaryTree(root: TreeNode | null): number {
242241
use std::cell::RefCell;
243242
use std::rc::Rc;
244243
impl Solution {
245-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
246-
if root.is_none() {
244+
pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
245+
let mut ans = 0;
246+
fn dfs(root: Option<Rc<RefCell<TreeNode>>>, ans: &mut i32) -> i32 {
247+
match root {
248+
Some(node) => {
249+
let node = node.borrow();
250+
let l = dfs(node.left.clone(), ans);
251+
let r = dfs(node.right.clone(), ans);
252+
253+
*ans = (*ans).max(l + r);
254+
255+
1 + l.max(r)
256+
}
257+
None => 0,
258+
}
259+
}
260+
dfs(root, &mut ans);
261+
ans
262+
}
263+
}
264+
```
265+
266+
#### JavaScript
267+
268+
```js
269+
/**
270+
* Definition for a binary tree node.
271+
* function TreeNode(val, left, right) {
272+
* this.val = (val===undefined ? 0 : val)
273+
* this.left = (left===undefined ? null : left)
274+
* this.right = (right===undefined ? null : right)
275+
* }
276+
*/
277+
/**
278+
* @param {TreeNode} root
279+
* @return {number}
280+
*/
281+
var diameterOfBinaryTree = function (root) {
282+
let ans = 0;
283+
const dfs = root => {
284+
if (!root) {
247285
return 0;
248286
}
249-
let root = root.as_ref().unwrap().as_ref().borrow();
250-
let left = Self::dfs(&root.left, res);
251-
let right = Self::dfs(&root.right, res);
252-
*res = (*res).max(left + right);
253-
left.max(right) + 1
287+
const [l, r] = [dfs(root.left), dfs(root.right)];
288+
ans = Math.max(ans, l + r);
289+
return 1 + Math.max(l, r);
290+
};
291+
dfs(root);
292+
return ans;
293+
};
294+
```
295+
296+
#### C#
297+
298+
```cs
299+
/**
300+
* Definition for a binary tree node.
301+
* public class TreeNode {
302+
* public int val;
303+
* public TreeNode left;
304+
* public TreeNode right;
305+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
306+
* this.val = val;
307+
* this.left = left;
308+
* this.right = right;
309+
* }
310+
* }
311+
*/
312+
public class Solution {
313+
private int ans;
314+
315+
public int DiameterOfBinaryTree(TreeNode root) {
316+
dfs(root);
317+
return ans;
254318
}
255319

256-
pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
257-
let mut res = 0;
258-
Self::dfs(&root, &mut res);
259-
res
320+
private int dfs(TreeNode root) {
321+
if (root == null) {
322+
return 0;
323+
}
324+
int l = dfs(root.left);
325+
int r = dfs(root.right);
326+
ans = Math.Max(ans, l + r);
327+
return 1 + Math.Max(l, r);
260328
}
261329
}
262330
```
@@ -272,84 +340,27 @@ impl Solution {
272340
* struct TreeNode *right;
273341
* };
274342
*/
275-
276-
#define max(a, b) (((a) > (b)) ? (a) : (b))
277-
278-
int dfs(struct TreeNode* root, int* res) {
279-
if (!root) {
343+
int dfs(struct TreeNode* root, int* ans) {
344+
if (root == NULL) {
280345
return 0;
281346
}
282-
int left = dfs(root->left, res);
283-
int right = dfs(root->right, res);
284-
*res = max(*res, left + right);
285-
return max(left, right) + 1;
347+
int l = dfs(root->left, ans);
348+
int r = dfs(root->right, ans);
349+
if (l + r > *ans) {
350+
*ans = l + r;
351+
}
352+
return 1 + (l > r ? l : r);
286353
}
287354

288355
int diameterOfBinaryTree(struct TreeNode* root) {
289-
int res = 0;
290-
dfs(root, &res);
291-
return res;
356+
int ans = 0;
357+
dfs(root, &ans);
358+
return ans;
292359
}
293360
```
294361
295362
<!-- tabs:end -->
296363
297364
<!-- solution:end -->
298365
299-
<!-- solution:start -->
300-
301-
### 方法二
302-
303-
<!-- tabs:start -->
304-
305-
#### Python3
306-
307-
```python
308-
# Definition for a binary tree node.
309-
# class TreeNode:
310-
# def __init__(self, val=0, left=None, right=None):
311-
# self.val = val
312-
# self.left = left
313-
# self.right = right
314-
class Solution:
315-
def diameterOfBinaryTree(self, root: TreeNode) -> int:
316-
def build(root):
317-
if root is None:
318-
return
319-
nonlocal d
320-
if root.left:
321-
d[root].add(root.left)
322-
d[root.left].add(root)
323-
if root.right:
324-
d[root].add(root.right)
325-
d[root.right].add(root)
326-
build(root.left)
327-
build(root.right)
328-
329-
def dfs(u, t):
330-
nonlocal ans, vis, d, next
331-
if u in vis:
332-
return
333-
vis.add(u)
334-
if t > ans:
335-
ans = t
336-
next = u
337-
for v in d[u]:
338-
dfs(v, t + 1)
339-
340-
d = defaultdict(set)
341-
ans = 0
342-
next = root
343-
build(root)
344-
vis = set()
345-
dfs(next, 0)
346-
vis.clear()
347-
dfs(next, 0)
348-
return ans
349-
```
350-
351-
<!-- tabs:end -->
352-
353-
<!-- solution:end -->
354-
355366
<!-- problem:end -->

0 commit comments

Comments
 (0)