Binary Tree Right Side View

简介: 好久没有刷leetcode了,要继续了。。 今天第一题。 Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

好久没有刷leetcode了,要继续了。。

今天第一题。

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

 

You should return [1, 3, 4].

一看到就觉得是层次遍历,而且每层的最后一个结点会保存下来。

C++实现代码:

#include<iostream>
#include<new>
#include<vector>
#include<queue>
using namespace std;

//Definition for binary tree
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) {
        if(root==NULL)
            return vector<int>();
        vector<int> res;
        queue<TreeNode*> q;
        q.push(root);
        int cur=1;
        while(!q.empty())
        {
            int rcur=0;
            while(cur)
            {
                TreeNode *tmp=q.front();
                q.pop();
                cur--;
                if(cur==0)
                    res.push_back(tmp->val);
                if(tmp->left)
                {
                    q.push(tmp->left);
                    rcur++;
                }
                if(tmp->right)
                {
                    q.push(tmp->right);
                    rcur++;
                }
            }
            cur=rcur;
        }
        return res;
    }
    void createTree(TreeNode *&root)
    {
        int i;
        cin>>i;
        if(i!=0)
        {
            root=new TreeNode(i);
            if(root==NULL)
                return;
            createTree(root->left);
            createTree(root->right);
        }
    }
};

int main()
{
    Solution s;
    TreeNode *root;
    s.createTree(root);
    vector<int> vec;
    vec=s.rightSideView(root);
    for(auto a:vec)
        cout<<a<<" ";
    cout<<endl;
}

 

相关文章
Leetcode 236. Lowest Common Ancestor of a Binary Tree
根据LCA的定义,二叉树中最小公共祖先就是两个节点p和q最近的共同祖先节点,LCA的定义没什么好解释的,主要是这道题的解法。
54 0
|
10月前
Transparent Data Encryption Data Dynamic and Data Dictionary Views You can query a set of dynamic and data dictionary views to find more information about Transparent Data Encryption (TDE) data.
Transparent Data Encryption Data Dynamic and Data Dictionary Views You can query a set of dynamic and data dictionary views to find more information about Transparent Data Encryption (TDE) data.
63 2
|
算法 Python
Leetcode-Medium 98. Validate Binary Search Tree
Leetcode-Medium 98. Validate Binary Search Tree
137 0
1110. Complete Binary Tree (25)
#include #include #include #include using namespace std; struct node { int ad, l, r; }; vector visited(20, ...
998 0
|
机器学习/深度学习
1064. Complete Binary Search Tree (30)
#include #include #include using namespace std; const int maxn = 1001; vector num(maxn), cbt(maxn); int n, c...
855 0
|
C语言
1102. Invert a Binary Tree (25)
#include #include #include using namespace std; vector in; struct node{ int l, r; }; vector tree; void inorder(int root){ if(tree[root].
839 0