Binary Tree Right Side View

原创 2015年11月20日 16:33:12
  1. 问题

    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].

  2. 解答
    class Solution1 {
    public:
        vector<int> rightSideView(TreeNode* root) {
            vector<int> res;
            
            while(root)
            {
                res.push_back(root->val);
                TreeNode *f1;
                
                if(root->right==NULL)
                {
                   f1=root->left;
                   if(f1==NULL) break;
                }
                else
                   f1=root->right;
                   
                root=f1;
            }
            
            return res;
        }
    };
    
    class Solution {
    public:
        vector<int> rightSideView(TreeNode *root) {
            vector<int> ans;
            if (root == nullptr) return ans;
            queue<TreeNode*> que;
            que.push(root);
            TreeNode* curr;
            while(!que.empty()) {
                int cnt = que.size();
                for (int i = 0; i < cnt; i++) {
                    curr = que.front(); que.pop();
                    if (curr->left) {
                        que.push(curr->left);
                    }
                    if (curr->right) {
                        que.push(curr->right);
                    }
                }
                ans.push_back(curr->val);
            }
            return ans;
        }
    };


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python

原题链接:199. Binary Tree Right Side View 【思路】非递归实现 层次遍历,每一层都是从左到右遍历,将每一层的最右一个节点添加到结果集中就是我们需要得到的结果: publ...

LeetCode: Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...

LeetCode #199 - Binary Tree Right Side View

题目描述: Given a binary tree, imagine yourself standing on the right side of it, return the values of ...
  • LawFile
  • LawFile
  • 2016年11月03日 23:17
  • 92

LeetCodeOJ_199_Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...

Leetcode: Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of...

LeetCode Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values o...

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

Leetcode 199 Binary Tree Right Side View 二叉树右视图

给出一个二叉树,设想你从树的右侧看过来,返回你从上往下可以看到的结点....

Binary Tree Right Side View

两种算法。 算法一:中序(但先右后左)dfs遍历,每次遍历一个节点时比较当前节点的高度与已看到的高度,若当前节点比已经看到的高度要高,则说明该节点是第一次被看到,加入到res中。此算法时间复杂度O(...

No199. Binary Tree Right Side View

一、题目描述 Given a binary tree, imagine yourself standing on the right side of it, return the values of...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Tree Right Side View
举报原因:
原因补充:

(最多只允许输入30个字)