LeetCode 515. Find Largest Value in Each Tree Row
来源:互联网 发布:网络的3类地址 编辑:程序博客网 时间:2024/06/06 03:51
515. Find Largest Value in Each Tree Row
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]
题意:
给你一颗二叉树,求每一层的最大值
代码:
/** * 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: //从根结点开始往下进行搜索,记录下每个节点的深度 void bfs(TreeNode*& root,int level,vector<int> &res){ if(!root)return ; if(res.size()<level+1)//如果当前深度的节点在数组中还没有记录的话,将该节点的值存入数组 res.push_back(root->val); else{ if(res[level]<root->val)//否则就更新为当前节点的值与数组中当前深度的值的最大值 res[level]=root->val; } if(root->left)bfs(root->left,level+1,res);//继续往下一层搜索 if(root->right)bfs(root->right,level+1,res); } vector<int> largestValues(TreeNode* root) { vector<int>res; bfs(root,0,res); return res; }};
阅读全文
0 0
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 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
- 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
- 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
- leetcode 515. Find Largest Value in Each Tree Row
- LeetCode 515. Find Largest Value in Each Tree Row (C++)
- 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
- LeetCode 515. Find Largest Value in Each Tree Row
- [LeetCode] 515. Find Largest Value in Each Tree Row
- 在java文件编写,编译,运行各个步骤中出现的常见的错误(1)
- 线性结构的表示和实现--我的图书馆
- Linux操作系统命令学习第一天
- [数据结构]数据结构——链表法
- Java练习
- LeetCode 515. Find Largest Value in Each Tree Row
- Sql 语句练习
- 第三周项目三 求集合并集
- Python2到Python3代码的转换工具2to3.py
- CSDN-markdown编辑器示意效果
- java程序练习:尝试创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将他向上转型到基类并调用这个方法。
- nginx配置location总结及rewrite规则写法
- JTable用法
- Navicat for mysql 下载、安装、连接MySQL操作