[DFS]515. Find Largest Value in Each Tree Row

原创 2017年03月28日 10:43:12

题目:

题目分析:

1、题目要求通过给出的二叉树找出每行最大值。最后通过一个容器返回所有结果。

2、这里注意找的是每一行的最大值,我们知道对于一个数组而言,最大值仅需遍历一遍即可;那么我们把每棵树当作若干数组的堆叠,仅需对每层进行一次遍历即可。很显然层序遍历的方法就是BFS。

3、明确了通过BFS求解后,还需要解决细节上的问题。比如,在BFS的队列实现中,如何找到各层的起始和终止位置。本题中我采用的方法就是设置两个计数量count_this、count_next分别保存对本层元素个数和下层元素个数。初始本层个数为1(树的根节点),下层个数置零。那么在BFS过程中每层都通过count_this的循环来遍历,每在队尾放入一个元素count_next++,并在每层统计出最大元素max放入答案容器中。每层遍历结束时,将count_next的值交给count_this,count_next再置零以完成层遍历的更迭。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include<queue>
 
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        queue<TreeNode*> Q;
        Q.push(root);
        int count_this = 1;
        int count_next = 0;
        vector<int> res;
        while(!Q.empty()){
        	int max = 0;
        	for(int i = 0; i < count_this; i++){
        		TreeNode* node = Q.front();
        		if(node->val > max)
        			max = node->val;
				if(node->left == NULL && node->right == NULL)
					continue;
				else{
					if(node->left != NULL && node->right == NULL){
						Q.push(node->left);
						count_next++;
					}
					else if(node->left == NULL && node->right != NULL){
						Q.push(node->right);
						count_next++;
					}
					else{
						Q.push(node->left);
						Q.push(node->right);
						count_next+=2;
					}
        		}
        		Q.pop();
			}
			res.push_back(max);
			count_this = count_next;
			count_next = 0;
        }
        return res;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

515. Find Largest Value in Each Tree Row

题目 You need to find the largest value in each row of a binary tree. Example: Input: ...

437. Path Sum III\515. Find Largest Value in Each Tree Row

Path Sum III 题目描述 代码实现 Find Largest Value in Each Tree Row 题目描述 代码实现437. Path Sum III题目描述You are giv...

LeetCode题解(Week5): 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 / ...

Leetcode515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row 1、原题 You need to find the largest value in each row of a ...

515. Find Largest Value in Each Tree Row(Medium)

原题目:   You need to find the largest value in each row of a binary tree.Example:Input: 1 ...

LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)

LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)

【LeetCode】515. Find Largest Value in Each Tree Row

问题描述问题链接:https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/descriptionYou need to ...
  • mrbcy
  • mrbcy
  • 2017年03月18日 21:08
  • 535

LeetCode笔记:515. Find Largest Value in Each Tree Row

寻找二叉树每一行的最大节点值

图(广度优先搜索)515. Find Largest Value in Each Tree Row[Middle]

题目: You need to find the largest value in each row of a binary tree. Example: Input: ...
  • xiegr
  • xiegr
  • 2017年03月18日 21:38
  • 99

Array(11) -- Largest Rectangle in Histogram, Maximal Rectangle, Find the Duplicate Number

Largest Rectangle in Histogram, Maximal Rectangle, Find the Duplicate Number
  • dwt0317
  • dwt0317
  • 2016年12月31日 22:03
  • 64
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[DFS]515. Find Largest Value in Each Tree Row
举报原因:
原因补充:

(最多只允许输入30个字)