Skip to content

Commit a2e850f

Browse files
author
wcp1231
committed
resolve 993
1 parent 0fd4c5a commit a2e850f

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mod s0993_cousins_in_binary_tree;
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* [993] Cousins in Binary Tree
3+
*
4+
* In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.
5+
*
6+
* Two nodes of a binary tree are cousins if they have the same depth, but have different parents.
7+
*
8+
* We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
9+
*
10+
* Return true if and only if the nodes corresponding to the values x and y are cousins.
11+
*
12+
*
13+
*
14+
* Example 1:<br />
15+
* <img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-01.png" style="width: 180px; height: 160px;" />
16+
*
17+
*
18+
* Input: root = <span id="example-input-1-1">[1,2,3,4]</span>, x = <span id="example-input-1-2">4</span>, y = <span id="example-input-1-3">3</span>
19+
* Output: <span id="example-output-1">false</span>
20+
*
21+
*
22+
* <div>
23+
* Example 2:<br />
24+
* <img alt="" src="https://assets.leetcode.com/uploads/2019/02/12/q1248-02.png" style="width: 201px; height: 160px;" />
25+
*
26+
*
27+
* Input: root = <span id="example-input-2-1">[1,2,3,null,4,null,5]</span>, x = <span id="example-input-2-2">5</span>, y = <span id="example-input-2-3">4</span>
28+
* Output: <span id="example-output-2">true</span>
29+
*
30+
*
31+
* <div>
32+
* Example 3:
33+
*
34+
* <img alt="" src="https://assets.leetcode.com/uploads/2019/02/13/q1248-03.png" style="width: 156px; height: 160px;" />
35+
*
36+
*
37+
* Input: root = <span id="example-input-3-1">[1,2,3,null,4]</span>, x = 2, y = 3
38+
* Output: <span id="example-output-3">false</span>
39+
*
40+
*
41+
* </div>
42+
* </div>
43+
*
44+
* Note:
45+
*
46+
* <ol>
47+
* The number of nodes in the tree will be between 2 and 100.
48+
* Each node has a unique integer value from 1 to 100.
49+
* </ol>
50+
*
51+
* <div>
52+
* <div>
53+
* <div> </div>
54+
* </div>
55+
* </div>
56+
*/
57+
pub struct Solution {}
58+
use crate::util::tree::{TreeNode, to_tree};
59+
60+
// problem: https://leetcode.com/problems/cousins-in-binary-tree/
61+
// discuss: https://leetcode.com/problems/cousins-in-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
62+
63+
// submission codes start here
64+
65+
// Definition for a binary tree node.
66+
// #[derive(Debug, PartialEq, Eq)]
67+
// pub struct TreeNode {
68+
// pub val: i32,
69+
// pub left: Option<Rc<RefCell<TreeNode>>>,
70+
// pub right: Option<Rc<RefCell<TreeNode>>>,
71+
// }
72+
//
73+
// impl TreeNode {
74+
// #[inline]
75+
// pub fn new(val: i32) -> Self {
76+
// TreeNode {
77+
// val,
78+
// left: None,
79+
// right: None
80+
// }
81+
// }
82+
// }
83+
use std::rc::Rc;
84+
use std::cell::{RefCell, Ref};
85+
86+
impl Solution {
87+
pub fn is_cousins(root: Option<Rc<RefCell<TreeNode>>>, x: i32, y: i32) -> bool {
88+
if x == y || root.is_none() {
89+
return false;
90+
}
91+
92+
let x_result: Option<(i32, i32)> = Solution::find_depth_and_parent(&root, x, 0);
93+
let y_result: Option<(i32, i32)> = Solution::find_depth_and_parent(&root, y, 0);
94+
95+
if x_result.is_none() || y_result.is_none() {
96+
return false;
97+
}
98+
99+
let x_result = x_result.unwrap();
100+
let y_result = y_result.unwrap();
101+
102+
if x_result.0 == y_result.0 && x_result.1 != y_result.1 {
103+
return true;
104+
}
105+
106+
false
107+
}
108+
109+
fn find_depth_and_parent(root: &Option<Rc<RefCell<TreeNode>>>, x: i32, depth: i32) -> Option<(i32, i32)> {
110+
let mut result = None;
111+
// TODO 太丑了
112+
if let Some(inner) = root {
113+
let val = inner.borrow().val;
114+
if let Some(l) = &inner.borrow().left {
115+
if *(&l.borrow().val) == x {
116+
result = Some((depth, val));
117+
}
118+
}
119+
if result.is_none() {
120+
if let Some(r) = &inner.borrow().right {
121+
if *(&r.borrow().val) == x {
122+
result = Some((depth, val));
123+
}
124+
}
125+
}
126+
if result.is_none() {
127+
result = Solution::find_depth_and_parent(&inner.borrow().left, x, depth + 1);
128+
}
129+
if result.is_none() {
130+
result = Solution::find_depth_and_parent(&inner.borrow().right, x, depth + 1);
131+
}
132+
}
133+
return result;
134+
}
135+
}
136+
137+
// submission codes end
138+
139+
#[cfg(test)]
140+
mod tests {
141+
use super::*;
142+
143+
#[test]
144+
fn test_993() {
145+
let root = to_tree(vec![Some(1),Some(2),Some(3), Some(4)]);
146+
let answer = Solution::is_cousins(root, 4, 3);
147+
assert!(!answer);
148+
149+
let root = to_tree(vec![Some(1),Some(2),Some(3), None, Some(4), None, Some(5)]);
150+
let answer = Solution::is_cousins(root, 4, 5);
151+
assert!(answer);
152+
153+
let root = to_tree(vec![Some(1),Some(2),Some(3), None, Some(4), None, Some(5)]);
154+
let answer = Solution::is_cousins(root, 2, 3);
155+
assert!(!answer);
156+
}
157+
}

0 commit comments

Comments
 (0)