LeetCode 199. 二叉树的右视图

Description:

题目大意:给定一个二叉树,输出其右视图。

解题思路:方法一:

算法标签:DFS

  1. 利用 DFS 先访问右节点,再访问左节点。
  2. 如果第一次访问对应深度的节点,说明这个节点是最右节点

代码:

/**
 * 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) {

        unordered_map<int ,int>rightview;
        int maxdepth = -1;

        stack<TreeNode*>tree;
        stack<int>treedepth;

        tree.push(root);
        treedepth.push(0);

        while(!tree.empty()) {
            TreeNode* node = tree.top();
            tree.pop();
            int depth = treedepth.top();
            treedepth.pop();

            if(node != NULL) {
                maxdepth = max(maxdepth , depth);
				
				// 如果没有找到对应深度的节点,说明其是最右节点
                if(rightview.find(depth) == rightview.end()) {
                    rightview[depth] = node -> val;
                }

                // 因为是栈结构,所以左子树先入栈,但是先访问的都是右子树
                tree.push(node -> left);
                tree.push(node -> right);
                treedepth.push(depth + 1);
                treedepth.push(depth + 1);
            }
        }

        vector<int>rightviewans;
        for(int i = 0;i <= maxdepth ;i++) {
            rightviewans.push_back(rightview[i]);
        }

        return rightviewans;
    }
};

解题思路:方法二:

算法标签: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) {

        unordered_map<int ,int>rightview;
        int maxdepth = -1;

        queue<TreeNode*>tree;
        queue<int>treedepth;

        tree.push(root);
        treedepth.push(0);

        while(!tree.empty()) {
            TreeNode* node = tree.front();
            tree.pop();
            int depth = treedepth.front();
            treedepth.pop();

            if(node != NULL) {
                maxdepth = max(maxdepth , depth);
                // 层序遍历最后访问的节点便是每一层的最右节点,因此不断更新即可
                rightview[depth] = node -> val;
                // 因为是栈结构,所以左子树先入栈,但是先访问的都是右子树
                tree.push(node -> left);
                tree.push(node -> right);
                treedepth.push(depth + 1);
                treedepth.push(depth + 1);
            }
        }

        vector<int>rightviewans;
        for(int i = 0;i <= maxdepth ;i++) {
            rightviewans.push_back(rightview[i]);
        }

        return rightviewans;
    }
};

你可能感兴趣的:(#,LeetCode,C++,leetcode)