687. Longest Univalue Path Medium
1/**
2 * [687] Longest Univalue Path
3 *
4 * Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass through the root.
5 * The length of the path between two nodes is represented by the number of edges between them.
6 *
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg" style="width: 450px; height: 238px;" />
9 * Input: root = [5,4,5,1,1,null,5]
10 * Output: 2
11 * Explanation: The shown image shows that the longest path of the same value (i.e. 5).
12 *
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg" style="width: 450px; height: 238px;" />
15 * Input: root = [1,4,5,4,4,null,5]
16 * Output: 2
17 * Explanation: The shown image shows that the longest path of the same value (i.e. 4).
18 *
19 *
20 * Constraints:
21 *
22 * The number of nodes in the tree is in the range [0, 10^4].
23 * -1000 <= Node.val <= 1000
24 * The depth of the tree will not exceed 1000.
25 *
26 */
27pub struct Solution {}
28use crate::util::tree::{TreeNode, to_tree};
29
30// problem: https://leetcode.com/problems/longest-univalue-path/
31// discuss: https://leetcode.com/problems/longest-univalue-path/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35// Definition for a binary tree node.
36// #[derive(Debug, PartialEq, Eq)]
37// pub struct TreeNode {
38// pub val: i32,
39// pub left: Option<Rc<RefCell<TreeNode>>>,
40// pub right: Option<Rc<RefCell<TreeNode>>>,
41// }
42//
43// impl TreeNode {
44// #[inline]
45// pub fn new(val: i32) -> Self {
46// TreeNode {
47// val,
48// left: None,
49// right: None
50// }
51// }
52// }
53use std::rc::Rc;
54use std::cell::RefCell;
55impl Solution {
56 pub fn longest_univalue_path(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_687() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.