Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 87b4bfe

Browse files
committed
feat: add solutions to lc problem: No.0965.Univalued Binary Tree
1 parent f1240f5 commit 87b4bfe

File tree

6 files changed

+297
-37
lines changed

6 files changed

+297
-37
lines changed

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

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<li>每个节点的值都是整数,范围为&nbsp;<code>[0, 99]</code>&nbsp;。</li>
3838
</ol>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
@@ -49,15 +48,120 @@
4948
<!-- 这里可写当前语言的特殊实现逻辑 -->
5049

5150
```python
52-
51+
# Definition for a binary tree node.
52+
# class TreeNode:
53+
# def __init__(self, val=0, left=None, right=None):
54+
# self.val = val
55+
# self.left = left
56+
# self.right = right
57+
class Solution:
58+
def isUnivalTree(self, root: TreeNode) -> bool:
59+
def dfs(root):
60+
if root is None:
61+
return True
62+
if root.val != self.val:
63+
return False
64+
return dfs(root.left) and dfs(root.right)
65+
66+
self.val = root.val
67+
return dfs(root)
5368
```
5469

5570
### **Java**
5671

5772
<!-- 这里可写当前语言的特殊实现逻辑 -->
5873

5974
```java
75+
/**
76+
* Definition for a binary tree node.
77+
* public class TreeNode {
78+
* int val;
79+
* TreeNode left;
80+
* TreeNode right;
81+
* TreeNode() {}
82+
* TreeNode(int val) { this.val = val; }
83+
* TreeNode(int val, TreeNode left, TreeNode right) {
84+
* this.val = val;
85+
* this.left = left;
86+
* this.right = right;
87+
* }
88+
* }
89+
*/
90+
class Solution {
91+
private int val;
92+
93+
public boolean isUnivalTree(TreeNode root) {
94+
val = root.val;
95+
return dfs(root);
96+
}
97+
98+
private boolean dfs(TreeNode root) {
99+
if (root == null) {
100+
return true;
101+
}
102+
if (root.val != val) {
103+
return false;
104+
}
105+
return dfs(root.left) && dfs(root.right);
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
/**
114+
* Definition for a binary tree node.
115+
* struct TreeNode {
116+
* int val;
117+
* TreeNode *left;
118+
* TreeNode *right;
119+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
121+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
122+
* };
123+
*/
124+
class Solution {
125+
public:
126+
int val;
127+
128+
bool isUnivalTree(TreeNode* root) {
129+
val = root->val;
130+
return dfs(root);
131+
}
132+
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);
137+
}
138+
};
139+
```
60140
141+
### **Go**
142+
143+
```go
144+
/**
145+
* Definition for a binary tree node.
146+
* type TreeNode struct {
147+
* Val int
148+
* Left *TreeNode
149+
* Right *TreeNode
150+
* }
151+
*/
152+
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
162+
}
163+
return dfs(root.Left, val) && dfs(root.Right, val)
164+
}
61165
```
62166

63167
### **...**

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

Lines changed: 106 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66

77
<p>A binary tree is <em>univalued</em> if every node in the tree has the same value.</p>
88

9-
10-
119
<p>Return <code>true</code>&nbsp;if and only if the given tree is univalued.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

17-
18-
1913
<p><strong>Example 1:</strong></p>
2014

2115
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0965.Univalued%20Binary%20Tree/images/unival_bst_1.png" style="width: 265px; height: 172px;" />
@@ -28,8 +22,6 @@
2822

2923
</pre>
3024

31-
32-
3325
<div>
3426

3527
<p><strong>Example 2:</strong></p>
@@ -46,37 +38,134 @@
4638

4739
</div>
4840

49-
50-
5141
<p>&nbsp;</p>
5242

53-
54-
5543
<p><strong>Note:</strong></p>
5644

57-
58-
5945
<ol>
6046
<li>The number of nodes in the given tree will be in the range <code>[1, 100]</code>.</li>
6147
<li>Each node&#39;s value will be an integer in the range <code>[0, 99]</code>.</li>
6248
</ol>
6349

64-
65-
6650
## Solutions
6751

6852
<!-- tabs:start -->
6953

7054
### **Python3**
7155

7256
```python
73-
57+
# Definition for a binary tree node.
58+
# class TreeNode:
59+
# def __init__(self, val=0, left=None, right=None):
60+
# self.val = val
61+
# self.left = left
62+
# self.right = right
63+
class Solution:
64+
def isUnivalTree(self, root: TreeNode) -> bool:
65+
def dfs(root):
66+
if root is None:
67+
return True
68+
if root.val != self.val:
69+
return False
70+
return dfs(root.left) and dfs(root.right)
71+
72+
self.val = root.val
73+
return dfs(root)
7474
```
7575

7676
### **Java**
7777

7878
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
private int val;
96+
97+
public boolean isUnivalTree(TreeNode root) {
98+
val = root.val;
99+
return dfs(root);
100+
}
101+
102+
private boolean dfs(TreeNode root) {
103+
if (root == null) {
104+
return true;
105+
}
106+
if (root.val != val) {
107+
return false;
108+
}
109+
return dfs(root.left) && dfs(root.right);
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
/**
118+
* Definition for a binary tree node.
119+
* struct TreeNode {
120+
* int val;
121+
* TreeNode *left;
122+
* TreeNode *right;
123+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
124+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
126+
* };
127+
*/
128+
class Solution {
129+
public:
130+
int val;
131+
132+
bool isUnivalTree(TreeNode* root) {
133+
val = root->val;
134+
return dfs(root);
135+
}
136+
137+
bool dfs(TreeNode* root) {
138+
if (root == nullptr) return true;
139+
if (root->val != val) return false;
140+
return dfs(root->left) && dfs(root->right);
141+
}
142+
};
143+
```
79144
145+
### **Go**
146+
147+
```go
148+
/**
149+
* Definition for a binary tree node.
150+
* type TreeNode struct {
151+
* Val int
152+
* Left *TreeNode
153+
* Right *TreeNode
154+
* }
155+
*/
156+
func isUnivalTree(root *TreeNode) bool {
157+
return dfs(root, root.Val)
158+
}
159+
160+
func dfs(root *TreeNode, val int) bool {
161+
if root == nil {
162+
return true
163+
}
164+
if root.Val != val {
165+
return false
166+
}
167+
return dfs(root.Left, val) && dfs(root.Right, val)
168+
}
80169
```
81170

82171
### **...**

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,23 @@
44
* int val;
55
* TreeNode *left;
66
* TreeNode *right;
7-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
810
* };
911
*/
1012
class Solution {
11-
private:
12-
bool isUnivalVal(TreeNode* r, int val)
13-
{
14-
if (val != r->val)
15-
return false ;
16-
17-
if (r->left && !isUnivalVal(r->left, val))
18-
return false ;
19-
if (r->right && !isUnivalVal(r->right, val))
20-
return false ;
21-
22-
return true ;
23-
}
2413
public:
14+
int val;
15+
2516
bool isUnivalTree(TreeNode* root) {
26-
if (nullptr == root)
27-
return true ;
28-
return isUnivalVal(root, root->val) ;
17+
val = root->val;
18+
return dfs(root);
19+
}
20+
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);
2925
}
30-
};
26+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
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
16+
}
17+
if root.Val != val {
18+
return false
19+
}
20+
return dfs(root.Left, val) && dfs(root.Right, val)
21+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /