LeetCode题解之Univalued Binary Tree
1、题目描述

2、问题分析
遍历一遍树,然后将所有节点的数值放入到一个set中,最后检查set中元素的个数是否为1.
3、代码
bool isUnivalTree(TreeNode* root) {
set<int> s;
preOrder(root, s);
return s.size() == ;
}
void preOrder(TreeNode* root, set<int> &s)
{
if (root == NULL)
return;
s.insert(root->val);
preOrder(root->left, s);
preOrder(root->right,s);
}
LeetCode题解之Univalued Binary Tree的更多相关文章
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【leetcode】965. Univalued Binary Tree
题目如下: A binary tree is univalued if every node in the tree has the same value. Return true if and on ...
- leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- LeetCode题解之Balanced Binary Tree
1.题目描述 2.问题分析 DFS. 3.代码 bool isBalanced(TreeNode* root) { if (root == NULL) return true; && ...
- LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的 ...
- 965. Univalued Binary Tree
题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...
- LeetCode Construct String from Binary Tree
原题链接在这里:https://leetcode.com/problems/construct-string-from-binary-tree/#/description 题目: You need t ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- 微信小程序图片变形解决方法
微信小程序的image标签中有个mode属性,使用aspectFill即可 注:image组件默认宽度300px.高度225px mode 有效值: mode 有 13 种模式,其中 4 种是缩放模式 ...
- Java NIO 基础知识
前言 前言部分是科普,读者可自行选择是否阅读这部分内容. 为什么我们需要关心 NIO?我想很多业务猿都会有这个疑问. 我在工作的前两年对这个问题也很不解,因为那个时候我认为自己已经非常熟悉 IO 操作 ...
- 搭建laravel到nginx
一.laravel的安装 搭建的第一步当然是安装好laravel,这里推介composer安装,由于国内的问题,极其推介使用国内的镜像去搭建,我在终端里本已经设置好常规的https和http之类的FQ ...
- PTA (Advanced Level) 1005 Spell It Right
Spell It Right Given a non-negative integer N, your task is to compute the sum of all the digits of ...
- gulp学习笔记-怎样做一个gulp-demo
第一步:在文件夹内:鼠标右键+shift 选择提示窗口中的 在此处打开命令窗口 第二步:创建npm的配置文件,在命令窗口中输入 npm init 进行npm的配置 npm init gulp-d ...
- MyBatis JavaType JdbcType
MyBatis 通过包含的jdbcType类型 BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED TINYINT REAL VARCHAR BINARY BLOB NV ...
- Java 8 新特性-菜鸟教程 (4) -Java 8 默认方法
Java 8 默认方法 Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不需要实现类去实现其方法. 我们只需在方法名前面加个default关键字即可实现默认方法. 为 ...
- APIO 2018 游记
上接CTSC 2018 游记 day1 早上大概八九点起来洗了个澡跑到隔壁寝发现 tj 还在??? 原来昨天晚上听错名字了... 下午一起去 wfj 王府井玩,陪李总逛逛奢侈品店... 走了两三个小时 ...
- Python面向对象基础一
公司可能过一两个月就要从深圳搬到东莞松山湖,项目组的现在有的在转Java或其他语言的,问我们要不要转java+hoodap+spark方向,我还是先不转,毕竟之前是从ios转回C#,这现在在转其他的那 ...
- vue---数据更新,视图不更新问题
写点赞功能时,点赞后已经追加到对象里了,但是视图没有更新. 查找了些资料: 数据已经更新了但是视图不更新的问题,有几个原因: 1.根属性不存在,而想要直接给根属性赋值导致的视图不更新.此时初始化属性的 ...