965:单值二叉树
This commit is contained in:
parent
aead51e257
commit
7ab68e8f84
81
src/main/java/leetcode/editor/cn/UnivaluedBinaryTree.java
Normal file
81
src/main/java/leetcode/editor/cn/UnivaluedBinaryTree.java
Normal file
@ -0,0 +1,81 @@
|
||||
//<p>如果二叉树每个节点都具有相同的值,那么该二叉树就是<em>单值</em>二叉树。</p>
|
||||
//
|
||||
//<p>只有给定的树是单值二叉树时,才返回 <code>true</code>;否则返回 <code>false</code>。</p>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>示例 1:</strong></p>
|
||||
//
|
||||
//<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50104-pm.png" style="height: 159px; width: 200px;"></p>
|
||||
//
|
||||
//<pre><strong>输入:</strong>[1,1,1,1,1,null,1]
|
||||
//<strong>输出:</strong>true
|
||||
//</pre>
|
||||
//
|
||||
//<p><strong>示例 2:</strong></p>
|
||||
//
|
||||
//<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50050-pm.png" style="height: 158px; width: 200px;"></p>
|
||||
//
|
||||
//<pre><strong>输入:</strong>[2,2,2,5,2]
|
||||
//<strong>输出:</strong>false
|
||||
//</pre>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ol>
|
||||
// <li>给定树的节点数范围是 <code>[1, 100]</code>。</li>
|
||||
// <li>每个节点的值都是整数,范围为 <code>[0, 99]</code> 。</li>
|
||||
//</ol>
|
||||
//<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 121</li><li>👎 0</li></div>
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TreeNode;
|
||||
|
||||
// 965:单值二叉树
|
||||
public class UnivaluedBinaryTree {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new UnivaluedBinaryTree().new Solution();
|
||||
// TO TEST
|
||||
}
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
public boolean isUnivalTree(TreeNode root) {
|
||||
return isSame(root, root.val);
|
||||
}
|
||||
|
||||
private boolean isSame(TreeNode node, int val) {
|
||||
if (node == null) {
|
||||
return true;
|
||||
}
|
||||
if (node.val != val) {
|
||||
return false;
|
||||
}
|
||||
boolean is = true;
|
||||
is = is && isSame(node.left, val);
|
||||
if (!is) {
|
||||
return false;
|
||||
}
|
||||
return is && isSame(node.right, val);
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<p>如果二叉树每个节点都具有相同的值,那么该二叉树就是<em>单值</em>二叉树。</p>
|
||||
|
||||
<p>只有给定的树是单值二叉树时,才返回 <code>true</code>;否则返回 <code>false</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50104-pm.png" style="height: 159px; width: 200px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>[1,1,1,1,1,null,1]
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/29/screen-shot-2018-12-25-at-50050-pm.png" style="height: 158px; width: 200px;"></p>
|
||||
|
||||
<pre><strong>输入:</strong>[2,2,2,5,2]
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>给定树的节点数范围是 <code>[1, 100]</code>。</li>
|
||||
<li>每个节点的值都是整数,范围为 <code>[0, 99]</code> 。</li>
|
||||
</ol>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 121</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user