【LeetCode-Tree】 Right view of binary tree

Title description

Given a binary tree, imagine yourself standing on the right side of it, returning the node values ​​that can be seen from the right side, in order from top to bottom.
Examples:

输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:

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

Title link: https://leetcode-cn.com/problems/binary-tree-right-side-view/

Idea 1

Use bfs. The right view of the binary tree is the rightmost node of each layer, so iterate through the hierarchy and take the rightmost node of each layer into the result. code show as below:

/**
 * 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) {
        if(root==nullptr) return {};

        queue<TreeNode*> q;
        q.push(root);
        int curLevelNums = 1;
        int nextLevelNums = 0;
        vector<int> ans;
        while(!q.empty()){
            TreeNode* node = q.front(); q.pop();
            if(curLevelNums==1) ans.push_back(node->val);
            curLevelNums--;
            if(node->left!=nullptr){
                q.push(node->left);
                nextLevelNums++;
            }
            if(node->right!=nullptr){
                q.push(node->right);
                nextLevelNums++;
            }

            if(curLevelNums==0){
                curLevelNums = nextLevelNums;
                nextLevelNums = 0;
            }
        }
        return ans;
    }

    void search(TreeNode* root, vector<int>& ans){
        if(root==nullptr) return;
        ans.push_back(root->val);
        if(root->right!=nullptr){
            search(root->right, ans);
        }else{
            search(root->left, ans);
        }
    }
};
  • Time complexity: O (n)
    Each node is traversed once, n is the number of nodes.
  • Space complexity: O (n)
    queue length is at most n.

Idea 2

Use dfs. First walk the root node, then the right subtree, and finally the left subtree, so that for each layer, the rightmost node is the first to reach, you can judge whether a new layer is reached by depth. code show as below:

/**
 * 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) {
        if(root==nullptr) return {};

        vector<int> ans;
        int depth = 0;
        search(root, depth, ans);
        return ans;
    }

    void search(TreeNode* root, int depth, vector<int>& ans){
        if(root==nullptr) return;
        
        if(ans.size()==depth){  // 到了新的一层
            ans.push_back(root->val);
        }
        search(root->right, depth+1, ans);
        search(root->left, depth+1, ans);
    }
};
  • Time complexity: O (n)
  • Space complexity: O (h)

Guess you like

Origin www.cnblogs.com/flix/p/12756163.html