zoukankan      html  css  js  c++  java
  • 【LeetCode-树】二叉树的右视图

    题目描述

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
    示例:

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

    题目链接: https://leetcode-cn.com/problems/binary-tree-right-side-view/

    思路1

    使用bfs。二叉树的右视图就是每一层最右边的节点,所以层次遍历并取每一层的最右节点放入结果中即可。代码如下:

    /**
     * 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);
            }
        }
    };
    
    • 时间复杂度:O(n)
      每个节点都遍历一遍,n为节点个数。
    • 空间复杂度:O(n)
      队列长度最多为n。

    思路2

    使用dfs。先走根结点,然后走右子树,最后走左子树,这样对于每一层,最右边的节点是最先走到的,可以通过深度来判断是否到达了一个新层。代码如下:

    /**
     * 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);
        }
    };
    
    • 时间复杂度:O(n)
    • 空间复杂度:O(h)
  • 相关阅读:
    用迁移学习创造的通用语言模型ULMFiT,达到了文本分类的最佳水平
    论文分享|《Universal Language Model Fine-tuning for Text Classificatio》
    tesnorflow实现N个epoch训练数据读取的办法
    理解TensorFlow的Queue
    tensorflow基本操作介绍
    前端框架 Vue 初探
    LeetCode 292 Nim Game(Nim游戏)
    设计模式有感
    说说翻转的数据结构课
    java 中xml转换为json对象
  • 原文地址:https://www.cnblogs.com/flix/p/12756163.html
Copyright © 2011-2022 走看看