zoukankan      html  css  js  c++  java
  • 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值。
    示例:
    输入:
              1
             / 
            3   2
           /      
          5   3    9
    输出: [1, 3, 9]

    详见:https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/

    C++:

    /**
     * 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) {
            if(!root)
            {
                return {};
            }
            vector<int> res;
            queue<TreeNode*> que;
            que.push(root);
            int cur=1;
            int mx=INT_MIN;
            while(!que.empty())
            {
                root=que.front();
                que.pop();
                --cur;
                if(root->val>mx)
                {
                    mx=root->val;
                }
                if(root->left)
                {
                    que.push(root->left);
                }
                if(root->right)
                {
                    que.push(root->right);
                }
                if(cur==0)
                {
                    cur=que.size();
                    res.push_back(mx);
                    mx=INT_MIN;
                }
            }
            return res;
        }
    };
    
  • 相关阅读:
    获取comboBox里面的item使用的方法
    QT格式化代码
    按键槽的写法
    int to String
    sprintf在51单片机中的使用
    学习使用MarkDown
    分享9款超酷的jQuery/CSS3插件
    2014年展望
    操作系统面试
    web一点小结
  • 原文地址:https://www.cnblogs.com/xidian2014/p/8908032.html
Copyright © 2011-2022 走看看