[LeetCode] 199. Binary Tree Right Side View

来源:互联网 发布:淘宝店招素材免费下载 编辑:程序博客网 时间:2024/05/20 13:04

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);      // 搜索左子树    }};


原创粉丝点击