Skip to content

Commit ad49ba9

Browse files
authored
feat: add solutions to lc problems: No.0965,0978 (#3954)
1 parent 9373d73 commit ad49ba9

File tree

16 files changed

+259
-108
lines changed

16 files changed

+259
-108
lines changed

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

+49-31
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一
59+
### 方法一:DFS
60+
61+
我们记根节点的值为 $x$,然后设计一个函数 $\text{dfs}(\text{root})$,它表示当前节点的值是否等于 $x$,并且它的左右子树也是单值二叉树。
62+
63+
在函数 $\text{dfs}(\text{root})$ 中,如果当前节点为空,那么返回 $\text{true}$,否则,如果当前节点的值等于 $x$,并且它的左右子树也是单值二叉树,那么返回 $\text{true}$,否则返回 $\text{false}$。
64+
65+
在主函数中,我们调用 $\text{dfs}(\text{root})$,并返回结果。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树中的节点数目。
6068

6169
<!-- tabs:start -->
6270

@@ -70,12 +78,13 @@ tags:
7078
# self.left = left
7179
# self.right = right
7280
class Solution:
73-
def isUnivalTree(self, root: TreeNode) -> bool:
74-
def dfs(node):
75-
if node is None:
81+
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
82+
def dfs(root: Optional[TreeNode]) -> bool:
83+
if root is None:
7684
return True
77-
return node.val == root.val and dfs(node.left) and dfs(node.right)
85+
return root.val == x and dfs(root.left) and dfs(root.right)
7886

87+
x = root.val
7988
return dfs(root)
8089
```
8190

@@ -98,15 +107,18 @@ class Solution:
98107
* }
99108
*/
100109
class Solution {
110+
private int x;
111+
101112
public boolean isUnivalTree(TreeNode root) {
102-
return dfs(root, root.val);
113+
x = root.val;
114+
return dfs(root);
103115
}
104116

105-
private boolean dfs(TreeNode root, int val) {
117+
private boolean dfs(TreeNode root) {
106118
if (root == null) {
107119
return true;
108120
}
109-
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
121+
return root.val == x && dfs(root.left) && dfs(root.right);
110122
}
111123
}
112124
```
@@ -128,12 +140,14 @@ class Solution {
128140
class Solution {
129141
public:
130142
bool isUnivalTree(TreeNode* root) {
131-
return dfs(root, root->val);
132-
}
133-
134-
bool dfs(TreeNode* root, int val) {
135-
if (!root) return true;
136-
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
143+
int x = root->val;
144+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
145+
if (!root) {
146+
return true;
147+
}
148+
return root->val == x && dfs(root->left) && dfs(root->right);
149+
};
150+
return dfs(root);
137151
}
138152
};
139153
```
@@ -150,12 +164,13 @@ public:
150164
* }
151165
*/
152166
func isUnivalTree(root *TreeNode) bool {
167+
x := root.Val
153168
var dfs func(*TreeNode) bool
154-
dfs = func(node *TreeNode) bool {
155-
if node == nil {
169+
dfs = func(root *TreeNode) bool {
170+
if root == nil {
156171
return true
157172
}
158-
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
173+
return root.Val == x && dfs(root.Left) && dfs(root.Right)
159174
}
160175
return dfs(root)
161176
}
@@ -179,14 +194,14 @@ func isUnivalTree(root *TreeNode) bool {
179194
*/
180195

181196
function isUnivalTree(root: TreeNode | null): boolean {
182-
const val = root.val;
183-
const dfs = (root: TreeNode | null) => {
184-
if (root == null) {
197+
const x = root!.val;
198+
const dfs = (root: TreeNode | null): boolean => {
199+
if (!root) {
185200
return true;
186201
}
187-
return root.val === val && dfs(root.left) && dfs(root.right);
202+
return root.val === x && dfs(root.left) && dfs(root.right);
188203
};
189-
return dfs(root.left) && dfs(root.right);
204+
return dfs(root);
190205
}
191206
```
192207

@@ -214,16 +229,19 @@ function isUnivalTree(root: TreeNode | null): boolean {
214229
use std::cell::RefCell;
215230
use std::rc::Rc;
216231
impl Solution {
217-
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
218-
if root.is_none() {
219-
return true;
220-
}
221-
let root = root.as_ref().unwrap().borrow();
222-
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
223-
}
224232
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
225-
let root = root.as_ref().unwrap().borrow();
226-
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
233+
let x = root.as_ref().unwrap().borrow().val;
234+
235+
fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
236+
if let Some(n) = node {
237+
let n = n.borrow();
238+
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
239+
} else {
240+
true
241+
}
242+
}
243+
244+
dfs(root, x)
227245
}
228246
}
229247
```

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

+49-31
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: DFS
56+
57+
We denote the value of the root node as $x$, and then design a function $\text{dfs}(\text{root})$, which indicates whether the current node's value is equal to $x$ and its left and right subtrees are also univalued binary trees.
58+
59+
In the function $\text{dfs}(\text{root})$, if the current node is null, return $\text{true}$; otherwise, if the current node's value is equal to $x$ and its left and right subtrees are also univalued binary trees, return $\text{true}$; otherwise, return $\text{false}$.
60+
61+
In the main function, we call $\text{dfs}(\text{root})$ and return the result.
62+
63+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the tree.
5664

5765
<!-- tabs:start -->
5866

@@ -66,12 +74,13 @@ tags:
6674
# self.left = left
6775
# self.right = right
6876
class Solution:
69-
def isUnivalTree(self, root: TreeNode) -> bool:
70-
def dfs(node):
71-
if node is None:
77+
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
78+
def dfs(root: Optional[TreeNode]) -> bool:
79+
if root is None:
7280
return True
73-
return node.val == root.val and dfs(node.left) and dfs(node.right)
81+
return root.val == x and dfs(root.left) and dfs(root.right)
7482

83+
x = root.val
7584
return dfs(root)
7685
```
7786

@@ -94,15 +103,18 @@ class Solution:
94103
* }
95104
*/
96105
class Solution {
106+
private int x;
107+
97108
public boolean isUnivalTree(TreeNode root) {
98-
return dfs(root, root.val);
109+
x = root.val;
110+
return dfs(root);
99111
}
100112

101-
private boolean dfs(TreeNode root, int val) {
113+
private boolean dfs(TreeNode root) {
102114
if (root == null) {
103115
return true;
104116
}
105-
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
117+
return root.val == x && dfs(root.left) && dfs(root.right);
106118
}
107119
}
108120
```
@@ -124,12 +136,14 @@ class Solution {
124136
class Solution {
125137
public:
126138
bool isUnivalTree(TreeNode* root) {
127-
return dfs(root, root->val);
128-
}
129-
130-
bool dfs(TreeNode* root, int val) {
131-
if (!root) return true;
132-
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
139+
int x = root->val;
140+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
141+
if (!root) {
142+
return true;
143+
}
144+
return root->val == x && dfs(root->left) && dfs(root->right);
145+
};
146+
return dfs(root);
133147
}
134148
};
135149
```
@@ -146,12 +160,13 @@ public:
146160
* }
147161
*/
148162
func isUnivalTree(root *TreeNode) bool {
163+
x := root.Val
149164
var dfs func(*TreeNode) bool
150-
dfs = func(node *TreeNode) bool {
151-
if node == nil {
165+
dfs = func(root *TreeNode) bool {
166+
if root == nil {
152167
return true
153168
}
154-
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
169+
return root.Val == x && dfs(root.Left) && dfs(root.Right)
155170
}
156171
return dfs(root)
157172
}
@@ -175,14 +190,14 @@ func isUnivalTree(root *TreeNode) bool {
175190
*/
176191

177192
function isUnivalTree(root: TreeNode | null): boolean {
178-
const val = root.val;
179-
const dfs = (root: TreeNode | null) => {
180-
if (root == null) {
193+
const x = root!.val;
194+
const dfs = (root: TreeNode | null): boolean => {
195+
if (!root) {
181196
return true;
182197
}
183-
return root.val === val && dfs(root.left) && dfs(root.right);
198+
return root.val === x && dfs(root.left) && dfs(root.right);
184199
};
185-
return dfs(root.left) && dfs(root.right);
200+
return dfs(root);
186201
}
187202
```
188203

@@ -210,16 +225,19 @@ function isUnivalTree(root: TreeNode | null): boolean {
210225
use std::cell::RefCell;
211226
use std::rc::Rc;
212227
impl Solution {
213-
fn dfs(val: i32, root: &Option<Rc<RefCell<TreeNode>>>) -> bool {
214-
if root.is_none() {
215-
return true;
216-
}
217-
let root = root.as_ref().unwrap().borrow();
218-
root.val == val && Self::dfs(val, &root.left) && Self::dfs(val, &root.right)
219-
}
220228
pub fn is_unival_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
221-
let root = root.as_ref().unwrap().borrow();
222-
Self::dfs(root.val, &root.left) && Self::dfs(root.val, &root.right)
229+
let x = root.as_ref().unwrap().borrow().val;
230+
231+
fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
232+
if let Some(n) = node {
233+
let n = n.borrow();
234+
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
235+
} else {
236+
true
237+
}
238+
}
239+
240+
dfs(root, x)
223241
}
224242
}
225243
```

solution/0900-0999/0965.Univalued Binary Tree/Solution.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
class Solution {
1313
public:
1414
bool isUnivalTree(TreeNode* root) {
15-
return dfs(root, root->val);
15+
int x = root->val;
16+
auto dfs = [&](this auto&& dfs, TreeNode* root) -> bool {
17+
if (!root) {
18+
return true;
19+
}
20+
return root->val == x && dfs(root->left) && dfs(root->right);
21+
};
22+
return dfs(root);
1623
}
17-
18-
bool dfs(TreeNode* root, int val) {
19-
if (!root) return true;
20-
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
21-
}
22-
};
24+
};

solution/0900-0999/0965.Univalued Binary Tree/Solution.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
* }
88
*/
99
func isUnivalTree(root *TreeNode) bool {
10+
x := root.Val
1011
var dfs func(*TreeNode) bool
11-
dfs = func(node *TreeNode) bool {
12-
if node == nil {
12+
dfs = func(root *TreeNode) bool {
13+
if root == nil {
1314
return true
1415
}
15-
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
16+
return root.Val == x && dfs(root.Left) && dfs(root.Right)
1617
}
1718
return dfs(root)
18-
}
19+
}

solution/0900-0999/0965.Univalued Binary Tree/Solution.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
* }
1515
*/
1616
class Solution {
17+
private int x;
18+
1719
public boolean isUnivalTree(TreeNode root) {
18-
return dfs(root, root.val);
20+
x = root.val;
21+
return dfs(root);
1922
}
2023

21-
private boolean dfs(TreeNode root, int val) {
24+
private boolean dfs(TreeNode root) {
2225
if (root == null) {
2326
return true;
2427
}
25-
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
28+
return root.val == x && dfs(root.left) && dfs(root.right);
2629
}
27-
}
30+
}

solution/0900-0999/0965.Univalued Binary Tree/Solution.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def isUnivalTree(self, root: TreeNode) -> bool:
9-
def dfs(node):
10-
if node is None:
8+
def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
9+
def dfs(root: Optional[TreeNode]) -> bool:
10+
if root is None:
1111
return True
12-
return node.val == root.val and dfs(node.left) and dfs(node.right)
12+
return root.val == x and dfs(root.left) and dfs(root.right)
1313

14+
x = root.val
1415
return dfs(root)

solution/0900-0999/0965.Univalued Binary Tree/Solution.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919
use std::cell::RefCell;
2020
use std::rc::Rc;
2121
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-
}
2922
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)
23+
let x = root.as_ref().unwrap().borrow().val;
24+
25+
fn dfs(node: Option<Rc<RefCell<TreeNode>>>, x: i32) -> bool {
26+
if let Some(n) = node {
27+
let n = n.borrow();
28+
n.val == x && dfs(n.left.clone(), x) && dfs(n.right.clone(), x)
29+
} else {
30+
true
31+
}
32+
}
33+
34+
dfs(root, x)
3235
}
3336
}

0 commit comments

Comments
 (0)