543. Diameter of Binary Tree Easy

@problem@discussion
#Tree#Depth-First Search#Binary Tree



1/**
2 * [543] Diameter of Binary Tree
3 *
4 * Given the root of a binary tree, return the length of the diameter of the tree.
5 * The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
6 * The length of a path between two nodes is represented by the number of edges between them.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg" style="width: 292px; height: 302px;" />
10 * Input: root = [1,2,3,4,5]
11 * Output: 3
12 * Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
13 * 
14 * Example 2:
15 * 
16 * Input: root = [1,2]
17 * Output: 1
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	The number of nodes in the tree is in the range [1, 10^4].
23 * 	-100 <= Node.val <= 100
24 * 
25 */
26pub struct Solution {}
27use crate::util::tree::{TreeNode, to_tree};
28
29// problem: https://leetcode.com/problems/diameter-of-binary-tree/
30// discuss: https://leetcode.com/problems/diameter-of-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34// Definition for a binary tree node.
35// #[derive(Debug, PartialEq, Eq)]
36// pub struct TreeNode {
37//   pub val: i32,
38//   pub left: Option<Rc<RefCell<TreeNode>>>,
39//   pub right: Option<Rc<RefCell<TreeNode>>>,
40// }
41// 
42// impl TreeNode {
43//   #[inline]
44//   pub fn new(val: i32) -> Self {
45//     TreeNode {
46//       val,
47//       left: None,
48//       right: None
49//     }
50//   }
51// }
52use std::rc::Rc;
53use std::cell::RefCell;
54impl Solution {
55    pub fn diameter_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
56        0
57    }
58}
59
60// submission codes end
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn test_543() {
68    }
69}
70


Back
© 2025 bowen.ge All Rights Reserved.