Leetcode 199.二叉树的右视图

文章目录

  • 题目
  • 代码实现(首刷自解)
  • 代码(8.21 二刷自解)
  • 代码(9.7 三刷自解)

题目

Leetcode 199.二叉树的右视图_第1张图片

代码实现(首刷自解)

Leetcode 199.二叉树的右视图

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        if(!root)   
            return res;
        queue<TreeNode*> dq;
        dq.push(root);
        while(!dq.empty()) {
            int sz = dq.size();
            for(int j = 1; j <= sz; j++) {
                auto front = dq.front();    // 取出头部
                if(j == sz) {
                    res.push_back(front->val);
                }
                if(front->left)
                    dq.push(front->left);
                if(front->right)
                    dq.push(front->right);
                dq.pop();
            }
        }
        return res;
    }
};

代码(8.21 二刷自解)

BM41 输出二叉树的右视图

#include 
class Solution {
public:
    unordered_map<int, int>map;
    vector<int> solve(vector<int>& preOrder, vector<int>& inOrder) {
        for(int i = 0; i < preOrder.size(); i++)
            map[inOrder[i]] = i;
        auto root = build(preOrder, 0, preOrder.size()-1, inOrder, 0, inOrder.size()-1);
        queue<TreeNode*> q;
        vector<int> res;
        q.push(root);
        while(!q.empty()) {
            int sz = q.size();
            for(int i = 0; i < sz; i++) {
                auto front = q.front();
                if(i == sz-1) {
                    res.push_back(front->val);
                }
                if(front->left) q.push(front->left);
                if(front->right) q.push(front->right);
                q.pop();
            }
        }
        return res;
    }
    TreeNode* build(vector<int>& preOrder, int preStart, int preEnd, vector<int>& inOrder, int inStart, int inEnd) {
        if(preStart > preEnd) {
            return nullptr;
        }
        int value = preOrder[preStart];
        int index = map[value];
        int len = index - inStart;
        auto root = new TreeNode(value);
        root->left = build(preOrder, preStart+1, preStart+len, inOrder, inStart, index - 1);
        root->right = build(preOrder, preStart+1+len, preEnd, inOrder, index+1, inEnd);
        return root;
    }
};

Leetcode版本 GO:

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func rightSideView(root *TreeNode) []int {
    res := []int{}
    if root == nil {
        return res
    }
    q := []*TreeNode{root}
    for len(q) > 0 {
        res = append(res, q[len(q)-1].Val)
        help := q
        q = nil
        for _, v := range help {
            if v.Left != nil {
                q = append(q, v.Left)
            }
            if v.Right != nil {
                q = append(q, v.Right)
            }
        }
        
    }
    return res
}

代码(9.7 三刷自解)

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        if(!root)
            return {};
        vector<int> res;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()) {
            int sz = q.size();
            for(int i = 0; i < sz; i++) {
                auto front = q.front();
                q.pop();
                if(i == sz-1) 
                    res.push_back(front->val);
                if(front->left) q.push(front->left);
                if(front->right) q.push(front->right);
            }
        }
        return res;
    }
};

你可能感兴趣的:(Leetcode专栏,leetcode,算法,数据结构)