515. Find Largest Value in Each Tree Row
来源:互联网 发布:沈阳数据库培训机构 编辑:程序博客网 时间:2024/06/02 03:26
You need to find the largest value in each row of a binary tree.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> largestValues(TreeNode* root) { queue<TreeNode *> qu; vector<int> result; TreeNode * node = NULL; if(root == NULL){ return result; } qu.push(root); while(!qu.empty()){ int n = qu.size(); int max = 0; TreeNode * node = qu.front(); max = node->val; for(int i = 0;i < n;++i){ TreeNode * node = qu.front(); if(node->val > max){ max = node->val; } if(node->left){ qu.push(node->left); } if(node->right){ qu.push(node->right); } qu.pop(); } result.push_back(max); } return result; }};
阅读全文
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
- kNN算法及其python实现
- python3 循环语句的位置应用(何时退出循环,何时在循环内写语句)
- 面试总结之Java中创建对象过程及方式
- Android Studio下对资源进行分包
- 车载微信助手,网页微信开发回顾
- 515. Find Largest Value in Each Tree Row
- 删除链表中带有重复值得节点
- Excel学习4_ java poi 读取有合并单元格的EXCEL文件
- 第5章 过渡
- mysql存储过程中 declare 和 set 定义变量的区别
- sklearn 的 Normalizer的L1和 L2
- 关于全排列的递归算法(Ruby实现)
- 数据库存长字符串
- python3花式秀操作--文件读写