Leader的专栏

这是一位Leader的博客

Binary Tree Right Side View

/**
 * 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> rightSideView(TreeNode* root) 
    {
        vector<int> res;
        if (!root)
            return res;
        queue<TreeNode*> q;
        q.push(root);
        TreeNode* cur = NULL;
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0; i < size; ++i)
            {
                cur = q.front();
                q.pop();
                if (cur->left)
                    q.push(cur->left);
                if (cur->right)
                    q.push(cur->right);
            }
            res.push_back(cur->val);
        }
        
        return res;
        
    }
};

阅读更多
个人分类: leetcode
上一篇Binary Tree Maximum Path Sum
下一篇Longest Valid Parentheses
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭