1315. Sum of Nodes with Even-Valued Grandparent Medium

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



1/**
2 * [1315] Sum of Nodes with Even-Valued Grandparent
3 *
4 * Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.
5 * A grandparent of a node is the parent of its parent if it exists.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/even1-tree.jpg" style="width: 504px; height: 302px;" />
9 * Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
10 * Output: 18
11 * Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/even2-tree.jpg" style="width: 64px; height: 65px;" />
15 * Input: root = [1]
16 * Output: 0
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	The number of nodes in the tree is in the range [1, 10^4].
22 * 	1 <= Node.val <= 100
23 * 
24 */
25pub struct Solution {}
26use crate::util::tree::{TreeNode, to_tree};
27
28// problem: https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/
29// discuss: https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33// Definition for a binary tree node.
34// #[derive(Debug, PartialEq, Eq)]
35// pub struct TreeNode {
36//   pub val: i32,
37//   pub left: Option<Rc<RefCell<TreeNode>>>,
38//   pub right: Option<Rc<RefCell<TreeNode>>>,
39// }
40// 
41// impl TreeNode {
42//   #[inline]
43//   pub fn new(val: i32) -> Self {
44//     TreeNode {
45//       val,
46//       left: None,
47//       right: None
48//     }
49//   }
50// }
51use std::rc::Rc;
52use std::cell::RefCell;
53impl Solution {
54    pub fn sum_even_grandparent(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
55        0
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_1315() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.