Skip to content

Commit 5df6c27

Browse files
committed
feat: add solutions to lc problems: No.0543, 0687
- No.0543.Diameter of Binary Tree - No.0687.Longest Univalue Path
1 parent d5e8663 commit 5df6c27

File tree

10 files changed

+699
-0
lines changed

10 files changed

+699
-0
lines changed

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

+108
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,114 @@ func max(a, b int) int {
219219
}
220220
```
221221

222+
### **C**
223+
224+
```c
225+
/**
226+
* Definition for a binary tree node.
227+
* struct TreeNode {
228+
* int val;
229+
* struct TreeNode *left;
230+
* struct TreeNode *right;
231+
* };
232+
*/
233+
234+
#define max(a,b) (((a) > (b)) ? (a) : (b))
235+
236+
int dfs(struct TreeNode *root, int *res) {
237+
if (!root) {
238+
return 0;
239+
}
240+
int left = dfs(root->left, res);
241+
int right = dfs(root->right, res);
242+
*res = max(*res, left + right);
243+
return max(left, right) + 1;
244+
}
245+
246+
int diameterOfBinaryTree(struct TreeNode *root) {
247+
int res = 0;
248+
dfs(root, &res);
249+
return res;
250+
}
251+
```
252+
253+
### **TypeScript**
254+
255+
```ts
256+
/**
257+
* Definition for a binary tree node.
258+
* class TreeNode {
259+
* val: number
260+
* left: TreeNode | null
261+
* right: TreeNode | null
262+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
263+
* this.val = (val===undefined ? 0 : val)
264+
* this.left = (left===undefined ? null : left)
265+
* this.right = (right===undefined ? null : right)
266+
* }
267+
* }
268+
*/
269+
270+
function diameterOfBinaryTree(root: TreeNode | null): number {
271+
let res = 0;
272+
const dfs = (root: TreeNode | null) => {
273+
if (root == null) {
274+
return 0;
275+
}
276+
const { left, right } = root;
277+
const l = dfs(left);
278+
const r = dfs(right);
279+
res = Math.max(res, l + r);
280+
return Math.max(l, r) + 1;
281+
};
282+
dfs(root);
283+
return res;
284+
}
285+
```
286+
287+
### **Rust**
288+
289+
```rust
290+
// Definition for a binary tree node.
291+
// #[derive(Debug, PartialEq, Eq)]
292+
// pub struct TreeNode {
293+
// pub val: i32,
294+
// pub left: Option<Rc<RefCell<TreeNode>>>,
295+
// pub right: Option<Rc<RefCell<TreeNode>>>,
296+
// }
297+
//
298+
// impl TreeNode {
299+
// #[inline]
300+
// pub fn new(val: i32) -> Self {
301+
// TreeNode {
302+
// val,
303+
// left: None,
304+
// right: None
305+
// }
306+
// }
307+
// }
308+
use std::rc::Rc;
309+
use std::cell::RefCell;
310+
impl Solution {
311+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
312+
if root.is_none() {
313+
return 0;
314+
}
315+
let root = root.as_ref().unwrap().as_ref().borrow();
316+
let left = Self::dfs(&root.left, res);
317+
let right = Self::dfs(&root.right, res);
318+
*res = (*res).max(left + right);
319+
left.max(right) + 1
320+
}
321+
322+
pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
323+
let mut res = 0;
324+
Self::dfs(&root, &mut res);
325+
res
326+
}
327+
}
328+
```
329+
222330
### **...**
223331

224332
```

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

+108
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,114 @@ func max(a, b int) int {
215215
}
216216
```
217217

218+
### **C**
219+
220+
```c
221+
/**
222+
* Definition for a binary tree node.
223+
* struct TreeNode {
224+
* int val;
225+
* struct TreeNode *left;
226+
* struct TreeNode *right;
227+
* };
228+
*/
229+
230+
#define max(a,b) (((a) > (b)) ? (a) : (b))
231+
232+
int dfs(struct TreeNode *root, int *res) {
233+
if (!root) {
234+
return 0;
235+
}
236+
int left = dfs(root->left, res);
237+
int right = dfs(root->right, res);
238+
*res = max(*res, left + right);
239+
return max(left, right) + 1;
240+
}
241+
242+
int diameterOfBinaryTree(struct TreeNode *root) {
243+
int res = 0;
244+
dfs(root, &res);
245+
return res;
246+
}
247+
```
248+
249+
### **TypeScript**
250+
251+
```ts
252+
/**
253+
* Definition for a binary tree node.
254+
* class TreeNode {
255+
* val: number
256+
* left: TreeNode | null
257+
* right: TreeNode | null
258+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
259+
* this.val = (val===undefined ? 0 : val)
260+
* this.left = (left===undefined ? null : left)
261+
* this.right = (right===undefined ? null : right)
262+
* }
263+
* }
264+
*/
265+
266+
function diameterOfBinaryTree(root: TreeNode | null): number {
267+
let res = 0;
268+
const dfs = (root: TreeNode | null) => {
269+
if (root == null) {
270+
return 0;
271+
}
272+
const { left, right } = root;
273+
const l = dfs(left);
274+
const r = dfs(right);
275+
res = Math.max(res, l + r);
276+
return Math.max(l, r) + 1;
277+
};
278+
dfs(root);
279+
return res;
280+
}
281+
```
282+
283+
### **Rust**
284+
285+
```rust
286+
// Definition for a binary tree node.
287+
// #[derive(Debug, PartialEq, Eq)]
288+
// pub struct TreeNode {
289+
// pub val: i32,
290+
// pub left: Option<Rc<RefCell<TreeNode>>>,
291+
// pub right: Option<Rc<RefCell<TreeNode>>>,
292+
// }
293+
//
294+
// impl TreeNode {
295+
// #[inline]
296+
// pub fn new(val: i32) -> Self {
297+
// TreeNode {
298+
// val,
299+
// left: None,
300+
// right: None
301+
// }
302+
// }
303+
// }
304+
use std::rc::Rc;
305+
use std::cell::RefCell;
306+
impl Solution {
307+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
308+
if root.is_none() {
309+
return 0;
310+
}
311+
let root = root.as_ref().unwrap().as_ref().borrow();
312+
let left = Self::dfs(&root.left, res);
313+
let right = Self::dfs(&root.right, res);
314+
*res = (*res).max(left + right);
315+
left.max(right) + 1
316+
}
317+
318+
pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
319+
let mut res = 0;
320+
Self::dfs(&root, &mut res);
321+
res
322+
}
323+
}
324+
```
325+
218326
### **...**
219327

220328
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
#define max(a,b) (((a) > (b)) ? (a) : (b))
11+
12+
int dfs(struct TreeNode *root, int *res) {
13+
if (!root) {
14+
return 0;
15+
}
16+
int left = dfs(root->left, res);
17+
int right = dfs(root->right, res);
18+
*res = max(*res, left + right);
19+
return max(left, right) + 1;
20+
}
21+
22+
int diameterOfBinaryTree(struct TreeNode *root) {
23+
int res = 0;
24+
dfs(root, &res);
25+
return res;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut i32) -> i32 {
23+
if root.is_none() {
24+
return 0;
25+
}
26+
let root = root.as_ref().unwrap().as_ref().borrow();
27+
let left = Self::dfs(&root.left, res);
28+
let right = Self::dfs(&root.right, res);
29+
*res = (*res).max(left + right);
30+
left.max(right) + 1
31+
}
32+
33+
pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
34+
let mut res = 0;
35+
Self::dfs(&root, &mut res);
36+
res
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function diameterOfBinaryTree(root: TreeNode | null): number {
16+
let res = 0;
17+
const dfs = (root: TreeNode | null) => {
18+
if (root == null) {
19+
return 0;
20+
}
21+
const { left, right } = root;
22+
const l = dfs(left);
23+
const r = dfs(right);
24+
res = Math.max(res, l + r);
25+
return Math.max(l, r) + 1;
26+
};
27+
dfs(root);
28+
return res;
29+
}

0 commit comments

Comments
 (0)