Skip to content

Commit 1b61d83

Browse files
committed
feat: update solutions to lc problem: No.0965
No.0965.Univalued Binary Tree
1 parent dd1a0c4 commit 1b61d83

File tree

6 files changed

+51
-99
lines changed

6 files changed

+51
-99
lines changed

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

+17-33
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@
5656
# self.right = right
5757
class Solution:
5858
def isUnivalTree(self, root: TreeNode) -> bool:
59-
def dfs(root):
60-
if root is None:
59+
def dfs(node):
60+
if node is None:
6161
return True
62-
if root.val != self.val:
63-
return False
64-
return dfs(root.left) and dfs(root.right)
62+
return node.val == root.val and dfs(node.left) and dfs(node.right)
6563

66-
self.val = root.val
6764
return dfs(root)
6865
```
6966

@@ -88,21 +85,15 @@ class Solution:
8885
* }
8986
*/
9087
class Solution {
91-
private int val;
92-
9388
public boolean isUnivalTree(TreeNode root) {
94-
val = root.val;
95-
return dfs(root);
89+
return dfs(root, root.val);
9690
}
9791

98-
private boolean dfs(TreeNode root) {
92+
private boolean dfs(TreeNode root, int val) {
9993
if (root == null) {
10094
return true;
10195
}
102-
if (root.val != val) {
103-
return false;
104-
}
105-
return dfs(root.left) && dfs(root.right);
96+
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
10697
}
10798
}
10899
```
@@ -123,17 +114,13 @@ class Solution {
123114
*/
124115
class Solution {
125116
public:
126-
int val;
127-
128117
bool isUnivalTree(TreeNode* root) {
129-
val = root->val;
130-
return dfs(root);
118+
return dfs(root, root->val);
131119
}
132120

133-
bool dfs(TreeNode* root) {
134-
if (root == nullptr) return true;
135-
if (root->val != val) return false;
136-
return dfs(root->left) && dfs(root->right);
121+
bool dfs(TreeNode* root, int val) {
122+
if (!root) return true;
123+
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
137124
}
138125
};
139126
```
@@ -150,17 +137,14 @@ public:
150137
* }
151138
*/
152139
func isUnivalTree(root *TreeNode) bool {
153-
return dfs(root, root.Val)
154-
}
155-
156-
func dfs(root *TreeNode, val int) bool {
157-
if root == nil {
158-
return true
159-
}
160-
if root.Val != val {
161-
return false
140+
var dfs func(*TreeNode) bool
141+
dfs = func(node *TreeNode) bool {
142+
if node == nil {
143+
return true
144+
}
145+
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
162146
}
163-
return dfs(root.Left, val) && dfs(root.Right, val)
147+
return dfs(root)
164148
}
165149
```
166150

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

+17-33
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,11 @@
4646
# self.right = right
4747
class Solution:
4848
def isUnivalTree(self, root: TreeNode) -> bool:
49-
def dfs(root):
50-
if root is None:
49+
def dfs(node):
50+
if node is None:
5151
return True
52-
if root.val != self.val:
53-
return False
54-
return dfs(root.left) and dfs(root.right)
52+
return node.val == root.val and dfs(node.left) and dfs(node.right)
5553

56-
self.val = root.val
5754
return dfs(root)
5855
```
5956

@@ -76,21 +73,15 @@ class Solution:
7673
* }
7774
*/
7875
class Solution {
79-
private int val;
80-
8176
public boolean isUnivalTree(TreeNode root) {
82-
val = root.val;
83-
return dfs(root);
77+
return dfs(root, root.val);
8478
}
8579

86-
private boolean dfs(TreeNode root) {
80+
private boolean dfs(TreeNode root, int val) {
8781
if (root == null) {
8882
return true;
8983
}
90-
if (root.val != val) {
91-
return false;
92-
}
93-
return dfs(root.left) && dfs(root.right);
84+
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
9485
}
9586
}
9687
```
@@ -111,17 +102,13 @@ class Solution {
111102
*/
112103
class Solution {
113104
public:
114-
int val;
115-
116105
bool isUnivalTree(TreeNode* root) {
117-
val = root->val;
118-
return dfs(root);
106+
return dfs(root, root->val);
119107
}
120108

121-
bool dfs(TreeNode* root) {
122-
if (root == nullptr) return true;
123-
if (root->val != val) return false;
124-
return dfs(root->left) && dfs(root->right);
109+
bool dfs(TreeNode* root, int val) {
110+
if (!root) return true;
111+
return root->val == val && dfs(root->left, val) && dfs(root->right, val);
125112
}
126113
};
127114
```
@@ -138,17 +125,14 @@ public:
138125
* }
139126
*/
140127
func isUnivalTree(root *TreeNode) bool {
141-
return dfs(root, root.Val)
142-
}
143-
144-
func dfs(root *TreeNode, val int) bool {
145-
if root == nil {
146-
return true
147-
}
148-
if root.Val != val {
149-
return false
128+
var dfs func(*TreeNode) bool
129+
dfs = func(node *TreeNode) bool {
130+
if node == nil {
131+
return true
132+
}
133+
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
150134
}
151-
return dfs(root.Left, val) && dfs(root.Right, val)
135+
return dfs(root)
152136
}
153137
```
154138

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@
1111
*/
1212
class Solution {
1313
public:
14-
int val;
15-
1614
bool isUnivalTree(TreeNode* root) {
17-
val = root->val;
18-
return dfs(root);
15+
return dfs(root, root->val);
1916
}
2017

21-
bool dfs(TreeNode* root) {
22-
if (root == nullptr) return true;
23-
if (root->val != val) return false;
24-
return dfs(root->left) && dfs(root->right);
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);
2521
}
2622
};

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
* }
88
*/
99
func isUnivalTree(root *TreeNode) bool {
10-
return dfs(root, root.Val)
11-
}
12-
13-
func dfs(root *TreeNode, val int) bool {
14-
if root == nil {
15-
return true
10+
var dfs func(*TreeNode) bool
11+
dfs = func(node *TreeNode) bool {
12+
if node == nil {
13+
return true
14+
}
15+
return node.Val == root.Val && dfs(node.Left) && dfs(node.Right)
1616
}
17-
if root.Val != val {
18-
return false
19-
}
20-
return dfs(root.Left, val) && dfs(root.Right, val)
17+
return dfs(root)
2118
}

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,14 @@
1414
* }
1515
*/
1616
class Solution {
17-
private int val;
18-
1917
public boolean isUnivalTree(TreeNode root) {
20-
val = root.val;
21-
return dfs(root);
18+
return dfs(root, root.val);
2219
}
2320

24-
private boolean dfs(TreeNode root) {
21+
private boolean dfs(TreeNode root, int val) {
2522
if (root == null) {
2623
return true;
2724
}
28-
if (root.val != val) {
29-
return false;
30-
}
31-
return dfs(root.left) && dfs(root.right);
25+
return root.val == val && dfs(root.left, val) && dfs(root.right, val);
3226
}
3327
}

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
# self.right = right
77
class Solution:
88
def isUnivalTree(self, root: TreeNode) -> bool:
9-
def dfs(root):
10-
if root is None:
9+
def dfs(node):
10+
if node is None:
1111
return True
12-
if root.val != self.val:
13-
return False
14-
return dfs(root.left) and dfs(root.right)
12+
return node.val == root.val and dfs(node.left) and dfs(node.right)
1513

16-
self.val = root.val
1714
return dfs(root)

0 commit comments

Comments
 (0)