199. Binary Tree Right Side View

来源:互联网 发布:淘宝联盟 阿里妈妈 编辑:程序博客网 时间:2024/06/04 18:42
/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //4ms acclass Solution {public:    vector<int> rightSideView(TreeNode* root) {        if(!root) return {};         queue<pair<TreeNode*,int>> q;        q.push(make_pair(root,1));        vector<int> v;        while(!q.empty())        {            TreeNode* temp=q.front().first;            int i=q.front().second;            if(temp->right) q.push(make_pair(temp->right,i+1));            if(temp->left)  q.push(make_pair(temp->left,i+1));            q.pop();            if(v.size()<i) v.push_back(temp->val);        }        return v;    }};
0 0
原创粉丝点击