Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0965.Univalued Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Aug 6, 2021
1 parent f1240f5 commit 87b4bfe
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 37 deletions.
108 changes: 106 additions & 2 deletions solution/0900-0999/0965.Univalued Binary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
<li>每个节点的值都是整数,范围为&nbsp;<code>[0, 99]</code>&nbsp;。</li>
</ol>


## 解法

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

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def dfs(root):
if root is None:
return True
if root.val != self.val:
return False
return dfs(root.left) and dfs(root.right)

self.val = root.val
return dfs(root)
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int val;

public boolean isUnivalTree(TreeNode root) {
val = root.val;
return dfs(root);
}

private boolean dfs(TreeNode root) {
if (root == null) {
return true;
}
if (root.val != val) {
return false;
}
return dfs(root.left) && dfs(root.right);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int val;

bool isUnivalTree(TreeNode* root) {
val = root->val;
return dfs(root);
}

bool dfs(TreeNode* root) {
if (root == nullptr) return true;
if (root->val != val) return false;
return dfs(root->left) && dfs(root->right);
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isUnivalTree(root *TreeNode) bool {
return dfs(root, root.Val)
}
func dfs(root *TreeNode, val int) bool {
if root == nil {
return true
}
if root.Val != val {
return false
}
return dfs(root.Left, val) && dfs(root.Right, val)
}
```

### **...**
Expand Down
123 changes: 106 additions & 17 deletions solution/0900-0999/0965.Univalued Binary Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@

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



<p>Return <code>true</code>&nbsp;if and only if the given tree is univalued.</p>



<p>&nbsp;</p>



<p><strong>Example 1:</strong></p>

<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;" />
Expand All @@ -28,8 +22,6 @@

</pre>



<div>

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

</div>



<p>&nbsp;</p>



<p><strong>Note:</strong></p>



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



## Solutions

<!-- tabs:start -->

### **Python3**

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def dfs(root):
if root is None:
return True
if root.val != self.val:
return False
return dfs(root.left) and dfs(root.right)

self.val = root.val
return dfs(root)
```

### **Java**

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private int val;

public boolean isUnivalTree(TreeNode root) {
val = root.val;
return dfs(root);
}

private boolean dfs(TreeNode root) {
if (root == null) {
return true;
}
if (root.val != val) {
return false;
}
return dfs(root.left) && dfs(root.right);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int val;

bool isUnivalTree(TreeNode* root) {
val = root->val;
return dfs(root);
}

bool dfs(TreeNode* root) {
if (root == nullptr) return true;
if (root->val != val) return false;
return dfs(root->left) && dfs(root->right);
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isUnivalTree(root *TreeNode) bool {
return dfs(root, root.Val)
}
func dfs(root *TreeNode, val int) bool {
if root == nil {
return true
}
if root.Val != val {
return false
}
return dfs(root.Left, val) && dfs(root.Right, val)
}
```

### **...**
Expand Down
32 changes: 14 additions & 18 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
bool isUnivalVal(TreeNode* r, int val)
{
if (val != r->val)
return false ;

if (r->left && !isUnivalVal(r->left, val))
return false ;
if (r->right && !isUnivalVal(r->right, val))
return false ;

return true ;
}
public:
int val;

bool isUnivalTree(TreeNode* root) {
if (nullptr == root)
return true ;
return isUnivalVal(root, root->val) ;
val = root->val;
return dfs(root);
}

bool dfs(TreeNode* root) {
if (root == nullptr) return true;
if (root->val != val) return false;
return dfs(root->left) && dfs(root->right);
}
} ;
};
21 changes: 21 additions & 0 deletions solution/0900-0999/0965.Univalued Binary Tree/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isUnivalTree(root *TreeNode) bool {
return dfs(root, root.Val)
}

func dfs(root *TreeNode, val int) bool {
if root == nil {
return true
}
if root.Val != val {
return false
}
return dfs(root.Left, val) && dfs(root.Right, val)
}

0 comments on commit 87b4bfe

Please sign in to comment.