2641. Cousins in Binary Tree II Medium

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



1/**
2 * [2641] Cousins in Binary Tree II
3 *
4 * Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values.
5 * Two nodes of a binary tree are cousins if they have the same depth with different parents.
6 * Return the root of the modified tree.
7 * Note that the depth of a node is the number of edges in the path from the root node to it.
8 *  
9 * <strong class="example">Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" />
11 * Input: root = [5,4,9,1,10,null,7]
12 * Output: [0,0,0,7,7,null,11]
13 * Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
14 * - Node with value 5 does not have any cousins so its sum is 0.
15 * - Node with value 4 does not have any cousins so its sum is 0.
16 * - Node with value 9 does not have any cousins so its sum is 0.
17 * - Node with value 1 has a cousin with value 7 so its sum is 7.
18 * - Node with value 10 has a cousin with value 7 so its sum is 7.
19 * - Node with value 7 has cousins with values 1 and 10 so its sum is 11.
20 * 
21 * <strong class="example">Example 2:
22 * <img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" />
23 * Input: root = [3,1,2]
24 * Output: [0,0,0]
25 * Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
26 * - Node with value 3 does not have any cousins so its sum is 0.
27 * - Node with value 1 does not have any cousins so its sum is 0.
28 * - Node with value 2 does not have any cousins so its sum is 0.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	The number of nodes in the tree is in the range [1, 10^5].
34 * 	1 <= Node.val <= 10^4
35 * 
36 */
37pub struct Solution {}
38use crate::util::tree::{TreeNode, to_tree};
39
40// problem: https://leetcode.com/problems/cousins-in-binary-tree-ii/
41// discuss: https://leetcode.com/problems/cousins-in-binary-tree-ii/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45// Definition for a binary tree node.
46// #[derive(Debug, PartialEq, Eq)]
47// pub struct TreeNode {
48//   pub val: i32,
49//   pub left: Option<Rc<RefCell<TreeNode>>>,
50//   pub right: Option<Rc<RefCell<TreeNode>>>,
51// }
52// 
53// impl TreeNode {
54//   #[inline]
55//   pub fn new(val: i32) -> Self {
56//     TreeNode {
57//       val,
58//       left: None,
59//       right: None
60//     }
61//   }
62// }
63use std::rc::Rc;
64use std::cell::RefCell;
65impl Solution {
66    pub fn replace_value_in_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
67        Some(Rc::new(RefCell::new(TreeNode::new(0))))
68    }
69}
70
71// submission codes end
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_2641() {
79    }
80}
81


Back
© 2025 bowen.ge All Rights Reserved.