-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49dc10e
commit 1a1bbf0
Showing
5 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
 | ||
``` | ||
输入:[1,1,1,1,1,null,1] | ||
输出:true | ||
``` | ||
示例 2: | ||
 | ||
``` | ||
输入:[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 原题链接和其他优选题解。 | ||
|