zoukankan      html  css  js  c++  java
  • #Leetcode# 199. Binary Tree Right Side View

    https://leetcode.com/problems/binary-tree-right-side-view/

    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.

    Example:

    Input: [1,2,3,null,5,null,4]
    Output: [1, 3, 4]
    Explanation:
    
       1            <---
     /   
    2     3         <---
          
      5     4       <---

    代码:

    /**
     * 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<vector<int> > levelOrder;
            vector<int> ans;
            if(!root) return ans;
            
            queue<pair<TreeNode*, int>> q;
            q.push(make_pair(root, 1));
            while(!q.empty()) {
                pair<TreeNode*, int> tp = q.front();
                q.pop();
                
                if(tp.second > levelOrder.size()) {
                    vector<int> v;
                    levelOrder.push_back(v);
                }
                
                levelOrder[tp.second - 1].push_back(tp.first->val);
                
                if(tp.first->left) 
                    q.push(make_pair(tp.first->left, tp.second + 1));
                
                if(tp.first->right) 
                    q.push(make_pair(tp.first->right, tp.second + 1));
            }
            
            
            for(int i = 0; i < levelOrder.size(); i ++) {
                ans.push_back(levelOrder[i][levelOrder[i].size() - 1]);
            }
            
            return ans;
        }
        
    };
    

    代码:

    /**
     * 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<vector<int> > levelOrder;
            vector<int> ans;
            if(!root) return ans;
            
            helper(root, levelOrder, 1);
            
            for(int i = 0; i < levelOrder.size(); i ++) {
                ans.push_back(levelOrder[i][levelOrder[i].size() - 1]);
            }
            
            return ans;
        }
        void helper(TreeNode* root, vector<vector<int> >& ans, int depth) {
          vector<int> v;
          if(depth > ans.size()) {
            ans.push_back(v);
            v.clear();
          }
          ans[depth - 1].push_back(root -> val);
          if(root -> left)
            helper(root -> left, ans, depth + 1);
          if(root -> right)
            helper(root -> right, ans, depth + 1);
        }
    };
    

      小张写的 code  感觉一晚上都在看层序遍历

  • 相关阅读:
    使用PHP调用TinyURL API的方法
    基于MySQL数据库的UTF8中文网站全文检索的实现
    PHP版单点登陆实现方案
    php框架codeigniter是如何实现mvc模式以及单一入口
    网友分享:二十五个顶级PHP模板引擎整理
    数据库置疑的处理办法
    使用Log Explorer恢复数据
    使用Log Explorer恢复数据
    11个故事 11个道理
    VS2008 WinForm 去掉DataGridView最后一行的那个空白行
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10133014.html
Copyright © 2011-2022 走看看