[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

Example 1:

Input: [1,1,1,1,1,null,1]
Output: true

Example 2:

Input: [2,2,2,5,2]
Output: false

Note:

  1. The number of nodes in the given tree will be in the range [1, 100].
  2. Each node‘s value will be an integer in the range [0, 99].


如果二叉树中的每个节点都具有相同的值,则二叉树是单值的。

如果且仅当给定树为单值时返回true。

例1:

输入:[1,1,1,1,1,null,1]

输出:真

例2:

输入:[2,2,2,5,2]

输出:假

注:

  1. 给定树中的节点数将在范围[1,100]内。
  2. 每个节点的值将是一个范围[0,99]内的整数。


12 ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var s:Set<Int> = Set<Int>()
16     func isUnivalTree(_ root: TreeNode?) -> Bool {
17         s.insert(root!.val)
18         if root?.left != nil
19         {
20              isUnivalTree(root!.left)
21         }
22         if root?.right != nil
23         {
24              isUnivalTree(root!.right)
25         }
26         return s.count == 1
27     }
28 }

原文地址:https://www.cnblogs.com/strengthen/p/10201416.html

时间: 2024-07-31 21:29:26

[Swift Weekly Contest 117]LeetCode965. 单值二叉树 | Univalued Binary Tree的相关文章

[Swift Weekly Contest 118]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal

Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this binary tree can be flipped by swapping the left child and the right child of that node. Consider the sequence of N values reported by a preorder traver

[Swift Weekly Contest 120]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total. In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or

[Swift Weekly Contest 117]LeetCode967. 具有相同连续差异的数字 | Numbers With Same Consecutive Differences

Return all non-negative integers of length N such that the absolute difference between every two consecutive digits is K. Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading

[Swift Weekly Contest 117]LeetCode966.元音拼写检查 | Vowel Spellchecker

Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word. For a given query word, the spell checker handles two categories of spelling mistakes: Capitalization: If the query matches a word in the wordlist (

Weekly Contest 117

965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. Return true if and only if the given tree is univalued. Example 1: Input: [1,1,1,1,1,null,1] Output: true Example 2: Input: [2,2,2,5,2] Output: false

遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R分别表示遍历左子树.访问根节点.遍历右子树 可能的情况6种 排列A3 2 LDR LRD DLR DRL RLD RDL 若限定先左后右 LDR LRD  中根序遍历  后根序遍历 DLR  先根序遍历 先/中/后 序遍历 原文地址:https://www.cnblogs.com/yuanjiangw/p

[Swift Weekly Contest 124]LeetCode993. 二叉树的堂兄弟节点 | Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique valu

[Swift Weekly Contest 127]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has

[Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /