515. Find Largest Value in Each Tree Row
来源:互联网 发布:Vb string 编辑:程序博客网 时间:2024/05/27 03:25
You need to find the largest value in each row of a binary tree.
Example:
Input:
1 / \ 3 2 / \ \ 5 3 9
Output: [1, 3, 9]
不明白这样的题目还是medium,所以直接提交代码了。。。
主要是利用了一个队列,然后每次找出一层的一个最大值,存在数组之中即可,依次知道队列中没有元素。
vector<int> largestValues(TreeNode* root) { vector<int> res; if (root == NULL)return res; queue<TreeNode *> data; data.push(root); while(!data.empty()){ int size = data.size(); int temp = INT_MIN; for (int i = 0; i < size; i++){ TreeNode *p = data.front(); data.pop(); temp = max(temp, p->val); if (p->left)data.push(p->left); if (p->right)data.push(p->right); } res.push_back(temp); } return res; }
阅读全文
0 0
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- week3-NO.515. Find Largest Value in Each Tree Row
- LeetCode笔记:515. Find Largest Value in Each Tree Row
- [leetcode] 515. Find Largest Value in Each Tree Row
- 【LeetCode】515. Find Largest Value in Each Tree Row
- 【AngularJS】model模型操作
- matlab函数
- 函数
- win7系统重装后没有声音怎么办 win7重装系统没有声音解决方法
- Essential Studio for UWP发布2017 v2,新增甘特图控件
- 515. Find Largest Value in Each Tree Row
- iconv编码转换
- [BZOJ 1061][Noi2008]志愿者招募:费用流|单纯型
- JSON简介及具体的JSON消息示例
- javascript刷新页面方法汇总
- C#网络编程(二、Socket基本操作)
- 初学HBuilder 之模拟器配置篇
- Java 网络编程API以及实例
- 2. JavaScript 设计模式(简单工厂,工厂方法,抽象工厂)