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

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

上面的可以简化。因为每层的最前的元素就是最右边的元素,如果是先压右儿子,后压左儿子的话。
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;
    }
};

你可能感兴趣的:(leetcode,深度优先,宽度优先)