Skip to content

Commit

Permalink
✨feat: Add 467 & 965
Browse files Browse the repository at this point in the history
  • Loading branch information
SharingSource committed May 25, 2022
1 parent 49dc10e commit 1a1bbf0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions Index/BFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
| [838. 推多米诺](https://leetcode-cn.com/problems/push-dominoes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/push-dominoes/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-y-z52w/) | 中等 | 🤩🤩🤩🤩 |
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1609. 奇偶树](https://leetcode-cn.com/problems/even-odd-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/even-odd-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-bfs-d-kuyi/) | 中等 | 🤩🤩🤩🤩🤩 |

1 change: 1 addition & 0 deletions Index/DFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
| [872. 叶子相似的树](https://leetcode-cn.com/problems/leaf-similar-trees/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/leaf-similar-trees/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-udfc/) | 简单 | 🤩🤩🤩 |
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩 |
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1239. 串联字符串的最大长度](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/solution/gong-shui-san-xie-yi-ti-san-jie-jian-zhi-nfeb/) | 中等 | 🤩🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
| [783. 二叉搜索树节点最小距离](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/solution/gong-shui-san-xie-yi-ti-san-jie-shu-de-s-7r17/) | 简单 | 🤩🤩🤩 |
| [863. 二叉树中所有距离为 K 的结点](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-jian-x6hak/) | 中等 | 🤩🤩🤩🤩 |
| [938. 二叉搜索树的范围和](https://leetcode-cn.com/problems/range-sum-of-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-of-bst/solution/gong-shui-san-xie-yi-ti-shuang-jie-di-gu-q2fo/) | 简单 | 🤩🤩🤩 |
| [965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/) | [LeetCode 题解链接](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/) | 简单 | 🤩🤩🤩🤩🤩 |
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩🤩🤩 |
| [993. 二叉树的堂兄弟节点](https://leetcode-cn.com/problems/cousins-in-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/cousins-in-binary-tree/solution/gong-shui-san-xie-shu-de-sou-suo-dfs-bfs-b200/) | 简单 | 🤩🤩 |
| [1104. 二叉树寻路](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-rw2d/) | 中等 | 🤩🤩🤩 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### 题目描述

这是 LeetCode 上的 **[467. 环绕字符串中唯一的子字符串](https://leetcode.cn/problems/can-i-win/solution/by-ac_oier-0ed9/)** ,难度为 **中等**
这是 LeetCode 上的 **[467. 环绕字符串中唯一的子字符串](https://leetcode.cn/problems/unique-substrings-in-wraparound-string/solution/by-ac_oier-qteu/)** ,难度为 **中等**

Tag : 「线性 DP」、「树状数组」

Expand Down
90 changes: 90 additions & 0 deletions LeetCode/961-970/965. 单值二叉树(简单).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
### 题目描述

这是 LeetCode 上的 **[965. 单值二叉树](https://leetcode.cn/problems/univalued-binary-tree/solution/by-ac_oier-zxjl/)** ,难度为 **简单**

Tag : 「DFS」、「BFS」、「二叉树」、「树的遍历」、「层序遍历」



如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。

只有给定的树是单值二叉树时,才返回 `true`;否则返回 `false`

示例 1:
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50104-pm.png)
```
输入:[1,1,1,1,1,null,1]
输出:true
```
示例 2:
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50050-pm.png)
```
输入:[2,2,2,5,2]
输出:false
```

提示:
* 给定树的节点数范围是 $[1, 100]¥。
* 每个节点的值都是整数,范围为 $[0, 99]$ 。

---

### 递归

根据题意进行模拟即可。

代码:
```Java
class Solution {
int val = -1;
public boolean isUnivalTree(TreeNode root) {
if (val == -1) val = root.val;
if (root == null) return true;
if (root.val != val) return false;
return isUnivalTree(root.left) && isUnivalTree(root.right);
}
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:忽略递归带来的额外空间消耗,复杂度为 $O(1)$

---

### 迭代

也可以使用迭代的方式进行层序遍历。

代码:
```Java
class Solution {
public boolean isUnivalTree(TreeNode root) {
int val = root.val;
Deque<TreeNode> d = new ArrayDeque<>();
d.addLast(root);
while (!d.isEmpty()) {
TreeNode poll = d.pollFirst();
if (poll.val != val) return false;
if (poll.left != null) d.addLast(poll.left);
if (poll.right != null) d.addLast(poll.right);
}
return true;
}
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.965` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

0 comments on commit 1a1bbf0

Please sign in to comment.