Skip to content

Commit dd1a0c4

Browse files
committedMay 23, 2022
feat: add solutions to lc problem: No.0965
No.0965.Univalued Binary Tree
1 parent f2348f2 commit dd1a0c4

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed
 

‎solution/0900-0999/0965.Univalued Binary Tree/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,73 @@ func dfs(root *TreeNode, val int) bool {
164164
}
165165
```
166166

167+
### **TypeScript**
168+
169+
```ts
170+
/**
171+
* Definition for a binary tree node.
172+
* class TreeNode {
173+
* val: number
174+
* left: TreeNode | null
175+
* right: TreeNode | null
176+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
177+
* this.val = (val===undefined ? 0 : val)
178+
* this.left = (left===undefined ? null : left)
179+
* this.right = (right===undefined ? null : right)
180+
* }
181+
* }
182+
*/
183+
184+
function isUnivalTree(root: TreeNode | null): boolean {
185+
const val = root.val;
186+
const dfs = (root: TreeNode | null) => {
187+
if (root == null) {
188+
return true;
189+
}
190+
return root.val === val && dfs(root.left) && dfs(root.right);
191+
};
192+
return dfs(root.left) && dfs(root.right);
193+
}
194+
```
195+
196+
### **Rust**
197+
198+
```rust
199+
// Definition for a binary tree node.
200+
// #[derive(Debug, PartialEq, Eq)]
201+
// pub struct TreeNode {
202+
// pub val: i32,
203+
// pub left: Option<Rc<RefCell<TreeNode>>>,
204+
// pub right: Option<Rc<RefCell<TreeNode>>>,
205+
// }
206+
//
207+
// impl TreeNode {
208+
// #[inline]
209+
// pub fn new(val: i32) -> Self {
210+
// TreeNode {
211+
// val,
212+
// left: None,
213+
// right: None
214+
// }
215+
// }
216+
// }
217+
use std::rc::Rc;
218+
use std::cell::RefCell;
219+
impl Solution {
220+
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
221+
if root.is_none() {
222+
return true;
223+
}
224+
let root = root.as_ref().unwrap().borrow();
225+
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
226+
}
227+
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
228+
let root = root.as_ref().unwrap().borrow();
229+
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
230+
}
231+
}
232+
```
233+
167234
### **...**
168235

169236
```

‎solution/0900-0999/0965.Univalued Binary Tree/README_EN.md

+67
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,73 @@ func dfs(root *TreeNode, val int) bool {
152152
}
153153
```
154154

155+
### **TypeScript**
156+
157+
```ts
158+
/**
159+
* Definition for a binary tree node.
160+
* class TreeNode {
161+
* val: number
162+
* left: TreeNode | null
163+
* right: TreeNode | null
164+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
165+
* this.val = (val===undefined ? 0 : val)
166+
* this.left = (left===undefined ? null : left)
167+
* this.right = (right===undefined ? null : right)
168+
* }
169+
* }
170+
*/
171+
172+
function isUnivalTree(root: TreeNode | null): boolean {
173+
const val = root.val;
174+
const dfs = (root: TreeNode | null) => {
175+
if (root == null) {
176+
return true;
177+
}
178+
return root.val === val && dfs(root.left) && dfs(root.right);
179+
};
180+
return dfs(root.left) && dfs(root.right);
181+
}
182+
```
183+
184+
### **Rust**
185+
186+
```rust
187+
// Definition for a binary tree node.
188+
// #[derive(Debug, PartialEq, Eq)]
189+
// pub struct TreeNode {
190+
// pub val: i32,
191+
// pub left: Option<Rc<RefCell<TreeNode>>>,
192+
// pub right: Option<Rc<RefCell<TreeNode>>>,
193+
// }
194+
//
195+
// impl TreeNode {
196+
// #[inline]
197+
// pub fn new(val: i32) -> Self {
198+
// TreeNode {
199+
// val,
200+
// left: None,
201+
// right: None
202+
// }
203+
// }
204+
// }
205+
use std::rc::Rc;
206+
use std::cell::RefCell;
207+
impl Solution {
208+
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
209+
if root.is_none() {
210+
return true;
211+
}
212+
let root = root.as_ref().unwrap().borrow();
213+
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
214+
}
215+
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
216+
let root = root.as_ref().unwrap().borrow();
217+
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
218+
}
219+
}
220+
```
221+
155222
### **...**
156223

157224
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
if root.is_none() {
24+
return true;
25+
}
26+
let root = root.as_ref().unwrap().borrow();
27+
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
28+
}
29+
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
30+
let root = root.as_ref().unwrap().borrow();
31+
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isUnivalTree(root: TreeNode | null): boolean {
16+
const val = root.val;
17+
const dfs = (root: TreeNode | null) => {
18+
if (root == null) {
19+
return true;
20+
}
21+
return root.val === val && dfs(root.left) && dfs(root.right);
22+
};
23+
return dfs(root.left) && dfs(root.right);
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.