#Leetcode# 199. Binary Tree Right Side View

二叉树右视图的层序遍历
本文详细介绍了如何使用层序遍历算法解决LeetCode上的二叉树右视图问题,通过两个不同的实现方式展示了如何从上到下返回可以看到的节点值。第一种方法使用队列进行广度优先搜索,第二种方法采用递归方式实现。两种方法均能有效获取二叉树每层最右侧节点的值。

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;
    }
    
};

  FH 写的 bfs 

代码:

/**
 * 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  感觉一晚上都在看层序遍历

转载于:https://www.cnblogs.com/zlrrrr/p/10133014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值