[LeetCode] 199. Binary Tree Right Side View

Problem:

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


解题分析:解此题的关键在于如何判断一个节点能否是"可见的",即如何判断一个节点是否应该push到

最终解的数组中。对每颗子树而言,当且仅当右节点为空时,左节点才有可能是"可见的",但这并不是

说,某颗子树的右节点为空时,其左节点就一定可见,因为这还受到与该左节点处于同一层的节点的影

响,举个栗子~观察下面的这颗二叉树:

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

若要使得5是"可见的",则与其处于同一层的值为1和4的节点必须为空,那么问题来了,如何判断一层中的某个节点

是否是可见的?

稍作分析不难知道,每一层最多只有一个节点是可见的,而且总是处于一层中最右边的节点是可见的,

所以,为了找出可见的所有节点,递归深度搜索每个节点作判断即可:

判断条件:当前可见节点数 <= 当前层数(从0开始计数) 即把该节点视为是可见的

深搜顺序:从右往左


Solution:

/**
 * 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> canSee;
        dfs(canSee, root, 0);
        return canSee;
    }
    
    void dfs(vector<int>& canSee, TreeNode* root, int level) {
        if (root == NULL) return;               // 边界
        if (canSee.size() <= level)
            canSee.push_back(root->val);
        dfs(canSee, root->right, level+1);     // 搜索右子树
        dfs(canSee, root->left, level+1);      // 搜索左子树
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值