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 ...
随机推荐
- JAVA NIO:Buffer.mark()的用法
在默认情况下,Buffer.mark()并没有什么用处,既不会影响Buffer的遍历,也不会影响Buffer的位置统计函数,如remaining()函数,能影响这些操作的行为只有position(). ...
- MVC返回文件
上一篇 介绍了Action 返回View, 顺便也看到了返回Json的处理, 这一篇并不看文件返回的源码, 此篇是为了应用. 1. Response返回文件 在MVC的项目中, 还是能看到很多同事, ...
- APIX招聘
- js实现四叉树算法
最近在看canvas动画方面教程,里面提到了采用四叉树检测碰撞.之前也看到过四叉树这个名词,但是一直不是很懂.于是就又找了一些四叉树方面的资料看了看,做个笔记,就算日后忘了,也可以回来看看. Quad ...
- .5-浅析express源码之Router模块(1)-默认中间件
模块application已经完结,开始讲Router路由部分. 切入口仍然在application模块中,方法就是那个随处可见的lazyrouter. 基本上除了初始化init方法,其余的app.u ...
- 使用Amazon AWS SNS 发送 SMS 消息 .net
1.浏览aws 开发人员指南 https://docs.aws.amazon.com/zh_cn/sns/latest/dg/sms_publish-to-phone.html 2.安装 aws sm ...
- SQL 拼接多个字段的值&一个字段多条记录的拼接
如student表: studentID studentName studentScore 01 Alice 90 02 Bill 95 03 Cindy 100 一.拼接多个字段的值 select ...
- java中那些类是线程安全的?
Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...
- javaer有福了,基于Web的mysql数据库管理工具TreeSoft发布了。
基于Web的数据库管理工具TreeSoft数据库管理系统发布快3年了,版本不断更快速迭代开发中,QQ群也积累了300多位粉丝,虽然时不时也掉粉,但总体还是增加中.大家虽然捐赠不多,但是苍蝇再小也是肉啊 ...
- Java - “JUC”锁
[Java并发编程实战]-----“J.U.C”:锁,lock 在java中有两种方法实现锁机制,一种是在前一篇博客中([java7并发编程实战]-----线程同步机制:synchronized) ...