1. 程式人生 > >LeetCode Binary Tree Right Side View

LeetCode Binary Tree Right Side View

層次遍歷

我的想法:層序遍歷,然後輸出每一層最右邊的節點程式碼如下:

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> result;
        vector<TreeNode*> high;
        vector<TreeNode*> low;
        if(root==NULL) return result;
        high.push_back(root);
        while(high.size()!=0){
            result.push_back(high[high.size()-1]->val);
            for(int i=0;i<high.size();i++){
                if(high[i]->left!=NULL) low.push_back(high[i]->left);
                if(high[i]->right!=NULL) low.push_back(high[i]->right);
            }
            high=low;
            low.clear();
        }
        return result;
    }
};
先序遍歷

參考討論區的回答後,採用了改進的先序遍歷來做,遍歷順序為根-》右子樹-》左子樹。當一個節點被遍歷到的時候,如果結果集中的節點數比當前的level值小的話,說明當前節點是本層中第一個被遍歷到的,因為我們先遍歷右子樹,所以當前節點一定是本層中最右節點。程式碼如下

class Solution {
public:
    void preOrder(TreeNode* root,int level,vector<int>&res){
        if(root==NULL) return;
        if(level>res.size()) res.push_back(root->val);
        preOrder(root->right,level+1,res);
        preOrder(root->left,level+1,res);
    }
    vector<int> rightSideView(TreeNode* root) {
        vector<int> res;
        preOrder(root,1,res);
        return res;
    }
};


相關推薦

LeetCode-Binary Tree Right Side View

一、Description Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered

[LeetCode] Binary Tree Right Side View 二叉樹的右側檢視

Given 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. For examp

[leetcode] Binary Tree Right Side View

dfs,記錄已經訪問到的層數即可。程式碼如下: vector<int> rightSideView(TreeNode *root) { vector<int> result; int curLevel = 0, lev

LeetCode Binary Tree Right Side View

層次遍歷 我的想法:層序遍歷,然後輸出每一層最右邊的節點程式碼如下: class Solution { public: vector<int> rightSideView(TreeNode* root) { vector<int&g

LeetCode Binary Tree Right Side View : 思想上的基於佇列的廣度優先遍歷,形式上的一個簡單變種

題目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top t

[LeetCode] 199. Binary Tree Right Side View

Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see o

[leetcode]199. Binary Tree Right Side View二叉樹右檢視

Given 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. E

[leetcode]199. Binary Tree Right Side View

[leetcode]199. Binary Tree Right Side View Analysis 好冷鴨—— [每天刷題並不難0.0] Given a binary tree, imagine yourself standing on the right

LeetCode 199.Binary Tree Right Side View (二叉樹的右檢視)

題目描述: 給定一棵二叉樹,想象自己站在它的右側,按照從頂部到底部的順序,返回從右側所能看到的節點值。 示例: 輸入: [1,2,3,null,5,null,4] 輸出: [1, 3, 4] 解釋: 1 <--- / \ 2

leetcode question 199:Binary Tree Right Side View

問題: Given a binary tree, imagine yourself standing on the right side of it, return the values of the

#Leetcode# 199. Binary Tree Right Side View

https://leetcode.com/problems/binary-tree-right-side-view/   Given a binary tree, imagine yourself standing on the right side of it, retur

JavaScript刷LeetCode -- 199. Binary Tree Right Side View [Medium]

一、題目  &emmsp;Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from

[LeetCode 199] Binary Tree Right Side View (遞迴的層數)

題目內容 199 Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of

LeetCode 199 Binary Tree Right Side View(二叉樹層序遍歷)

Given a binary tree, imagine yourself standing on therightside of it, return the values of the node

LeetCode】199. Binary Tree Right Side View

Problem:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from to

199. Binary Tree Right Side View

boolean des empty vid sin using tom als null 題目: Given a binary tree, imagine yourself standing on the right side of it, return the value

199. Binary Tree Right Side View -----層序遍歷

span ott ane sta esc style stand you question Given a binary tree, imagine yourself standing on the right side of it, return the values o

[Swift]LeetCode199. 二叉樹的右檢視 | Binary Tree Right Side View

Given 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. E

199 Binary Tree Right Side View

題目: Given a binary tree, imagine yourself standing on the right side of it, return the values of

199. Binary Tree Right Side View - Medium

lee plan ise edi mos example init solution cep Given a binary tree, imagine yourself standing on the right side of it, return the values