1. 程式人生 > >【題解】Binary Tree Right Side View

【題解】Binary Tree Right Side View

這道leetcode最新出的題,貌似也不是很難,用廣度優先搜尋即可,參考程式碼如下。

class Solution {
public:
    vector<int> rightSideView(TreeNode *root) {
        vector<int> answer;
		queue<TreeNode *> queue, temp_queue;
		TreeNode * front = 0;
		
		if (NULL == root)
		{
			return answer;
		}
		queue.push(root);
		answer.push_back(root->val);
		while (!queue.empty())
		{
			while (!temp_queue.empty())
			{
				temp_queue.pop();
			}
			while (!queue.empty())
			{
				front = queue.front();
				if (NULL != front->left)
				{
					temp_queue.push(front->left);
				}
				if (NULL != front->right)
				{
					temp_queue.push(front->right);
				}
				queue.pop();
			}
			queue = temp_queue;
			if (!queue.empty())
			{
				answer.push_back(queue.back()->val);
			}
			
		}
		return answer;
    }
};


相關推薦

題解Binary Tree Right Side View

這道leetcode最新出的題,貌似也不是很難,用廣度優先搜尋即可,參考程式碼如下。 class Solution { public: vector<int> rightSideView(TreeNode *root) { vector&

LeetCode199. 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

[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-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 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

[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

[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

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

199. Binary Tree Right Side ViewTree

連結:https://leetcode.com/problems/binary-tree-right-side-view/ 題目:返回每一層的最右節點。 思路:層次遍歷,每次統計當前層的節點個數(cur_low)和下一層的節點個數(next_low),當cur_low ==1 時

[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

Binary Tree Right Side View (是不是得寫篇字慶賀一下啊)

好像是中級題目以來,第一次自己思路一次就通過的的,紅紅火火恍恍惚惚哼哼哈嘿,是不是得寫篇字慶賀一下啊,雖說不難吧,紅紅火火恍恍惚惚哼哼哈嘿。 層級遍歷,列印每層的最後一個數字。 /** * Definition for a binary tree node. * pub