993. Cousins in Binary Tree Easy

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



1/**
2 * [993] Cousins in Binary Tree
3 *
4 * Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise.
5 * Two nodes of a binary tree are cousins if they have the same depth with different parents.
6 * Note that in a binary tree, the root node is at the depth 0, and children of each depth k node are at the depth k + 1.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-01.png" style="width: 304px; height: 270px;" />
10 * Input: root = [1,2,3,4], x = 4, y = 3
11 * Output: false
12 * 
13 * Example 2:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-02.png" style="width: 334px; height: 266px;" />
15 * Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
16 * Output: true
17 * 
18 * Example 3:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2019/02/13/q1248-03.png" style="width: 267px; height: 258px;" />
20 * Input: root = [1,2,3,null,4], x = 2, y = 3
21 * Output: false
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	The number of nodes in the tree is in the range [2, 100].
27 * 	1 <= Node.val <= 100
28 * 	Each node has a unique value.
29 * 	x != y
30 * 	x and y are exist in the tree.
31 * 
32 */
33pub struct Solution {}
34use crate::util::tree::{TreeNode, to_tree};
35
36// problem: https://leetcode.com/problems/cousins-in-binary-tree/
37// discuss: https://leetcode.com/problems/cousins-in-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41// Definition for a binary tree node.
42// #[derive(Debug, PartialEq, Eq)]
43// pub struct TreeNode {
44//   pub val: i32,
45//   pub left: Option<Rc<RefCell<TreeNode>>>,
46//   pub right: Option<Rc<RefCell<TreeNode>>>,
47// }
48// 
49// impl TreeNode {
50//   #[inline]
51//   pub fn new(val: i32) -> Self {
52//     TreeNode {
53//       val,
54//       left: None,
55//       right: None
56//     }
57//   }
58// }
59use std::rc::Rc;
60use std::cell::RefCell;
61impl Solution {
62    pub fn is_cousins(root: Option<Rc<RefCell<TreeNode>>>, x: i32, y: i32) -> bool {
63        false
64    }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_993() {
75    }
76}
77


Back
© 2025 bowen.ge All Rights Reserved.