• Leetcode 199. Binary Tree Right Side View (DFS/BFS好题)


    1. Binary Tree Right Side View
      Medium

    Given the root of 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 1:

    Input: root = [1,2,3,null,5,null,4]
    Output: [1,3,4]
    Example 2:

    Input: root = [1,null,3]
    Output: [1,3]
    Example 3:

    Input: root = []
    Output: []

    Constraints:

    The number of nodes in the tree is in the range [0, 100].
    -100 <= Node.val <= 100

    解法1:DFS遍历。
    注意: if (res.size() < depth) 表示这是这一层的最右边的节点

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            if (!root) return {};
            vector<int> res;
            helper(root, 1, res);
            return res;
        }
    private:
        int depth = 0;
        void helper(TreeNode* root, int depth, vector<int> &res) {
            if (!root) return;
            if (res.size() < depth) {
                res.push_back(root->val);
            }
            helper(root->right, depth + 1, res);
            helper(root->left, depth + 1, res);
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    解法2:BFS

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            vector<int> res;
            if (!root) return res;
            queue<TreeNode *> q;        
            q.push(root);
            res.push_back(root->val);
            while (!q.empty()) {
                int qSize = q.size();
                bool first = false;
                for (int i = 0; i < qSize; i++) {
                    TreeNode *frontNode = q.front();
                    q.pop();
                    if (frontNode->right) {
                        q.push(frontNode->right);
                        if (!first) {
                            res.push_back(frontNode->right->val);
                            first = true;
                        }
                    }
                    if (frontNode->left) {
                        q.push(frontNode->left);
                        if (!first) {
                            res.push_back(frontNode->left->val);
                            first = true;
                        }
                    }
                }
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    上面的可以简化。因为每层的最前的元素就是最右边的元素,如果是先压右儿子,后压左儿子的话。
    res.push_back(q.front()->val);

    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            vector<int> res;
            if (!root) return res;
            queue<TreeNode *> q;        
            q.push(root);
            while (!q.empty()) {
                res.push_back(q.front()->val);
                int qSize = q.size();
                for (int i = 0; i < qSize; i++) {
                    TreeNode *frontNode = q.front();
                    q.pop();
                    if (frontNode->right) {
                        q.push(frontNode->right);
                    }
                    if (frontNode->left) {
                        q.push(frontNode->left);
                    }
                }
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    【Linux】 man命令使用
    【POJ No. 3104】 烘干衣服 Drying
    分布式定时调度:xxl-job 万字详解
    Clickhouse—DDL 操作
    制作一个简单HTML宠物猫网页(HTML+CSS)
    微信小程序tab加列表demo
    springboot网络安全平台设计毕业设计源码042335
    SpringBoot SpringBoot 开发实用篇 5 整合第三方技术 5.10 jetcache 本地缓存方案
    修复版知宇发卡企业级发卡平台完整源码/多商户入驻+对接微信公众号+对接免签支付
    翻车了,被读者找出 BUG
  • 原文地址:https://blog.csdn.net/roufoo/article/details/134422637