[leetcode-515-Find Largest Value in Each Tree Row]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode-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]

思路:

层次遍历,每一层选出当前层最大值。

vector<int> largestValues(TreeNode* root)
     {
         vector<int>result;
         if (root == NULL)return result;
         queue<TreeNode*>que;
         que.push(root);
         int tempmax = INT_MIN;
         while (!que.empty())
         {
             int levelsize = que.size();
             TreeNode* temp;
             for (int i = 0; i < levelsize;i++)
             {
                 temp = que.front();
                 que.pop();
                 if (temp->left != NULL)que.push(temp->left);
                 if (temp->right != NULL)que.push(temp->right);
                 tempmax = max(tempmax, temp->val);
             }
             result.push_back(tempmax);
             tempmax = INT_MIN;
         }
         return result;         
     }

 









以上是关于[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(dfs)

(BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row

leetcode515- Find Largest Value in Each Tree Row- medium

P1667 数列(置换&前缀和)

堆排序 python