987. 二叉樹的垂序遍歷

給定二叉樹,按垂序遍歷返回其結點值。

對位于?(X, Y)?的每個結點而言,其左右子結點分別位于?(X-1, Y-1)?和?(X+1, Y-1)。

把一條垂線從?X = -infinity?移動到?X = +infinity?,每當該垂線與結點接觸時,我們按從上到下的順序報告結點的值( Y?坐標遞減)。

如果兩個結點位置相同,則首先報告的結點值較小。

按?X?坐標順序返回非空報告的列表。每個報告都有一個結點值列表。

?

示例 1:

輸入:[3,9,20,null,null,15,7]
輸出:[[9],[3,15],[20],[7]]
解釋:?
在不喪失其普遍性的情況下,我們可以假設根結點位于 (0, 0):
然后,值為 9 的結點出現在 (-1, -1);
值為 3 和 15 的兩個結點分別出現在 (0, 0) 和 (0, -2);
值為 20 的結點出現在 (1, -1);
值為 7 的結點出現在 (2, -2)。
示例 2:

輸入:[1,2,3,4,5,6,7]
輸出:[[4],[2],[1,5,6],[3],[7]]
解釋:
根據給定的方案,值為 5 和 6 的兩個結點出現在同一位置。
然而,在報告 "[1,5,6]" 中,結點值 5 排在前面,因為 5 小于 6。
?

提示:

樹的結點數介于 1?和?1000?之間。
每個結點值介于?0?和?1000?之間。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

解法:

?

/*** 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<vector<int>> verticalTraversal(TreeNode* root) {map<int, vector<int> > m; queue<pair<int, TreeNode*> > q; q.push(make_pair(0, root)); while (!q.empty()){set<pair<int, int> > tmp;  int len = q.size();for (int i = 0; i < len; ++i){auto p = q.front(); q.pop();tmp.insert(make_pair(p.first, p.second->val));if (p.second->left) q.push(make_pair(p.first - 1, p.second->left));if (p.second->right) q.push(make_pair(p.first + 1, p.second->right));}for (auto p : tmp) m[p.first].push_back(p.second);}vector<vector<int> > res;for (auto kv : m) res.push_back(kv.second);return res;}
};

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385131.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385131.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385131.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

28. 實現 strStr()

實現 strStr() 函數。 給定一個 haystack 字符串和一個 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在&#xff0c;則返回 -1。 示例 1: 輸入: haystack "hello", needle "ll" 輸出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a?i?? as a?k???a?1??a?0?? with 0 for all i and a?k??>0. Then N is palindromic if and only if a?i??a?k?i?? for all i. Zero is written 0 and is also palindrom…

1044 火星數字 (20 分)

火星人是以 13 進制計數的&#xff1a; 地球人的 0 被火星人稱為 tret。地球人數字 1 到 12 的火星文分別為&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人將進位以后的 12 個高位數字分別稱為&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

給定兩個以字符串形式表示的非負整數 num1 和 num2&#xff0c;返回 num1 和 num2 的乘積&#xff0c;它們的乘積也表示為字符串形式。 示例 1: 輸入: num1 "2", num2 "3" 輸出: "6" 示例 2: 輸入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一個經典的劃分過程&#xff1a;我們通常采用某種方法取一個元素作為主元&#xff0c;通過交換&#xff0c;把比主元小的元素放到它的左邊&#xff0c;比主元大的元素放到它的右邊。 給定劃分后的 N 個互不相同的正整數的排列&#xff0c;請問有多少個元…

1049 數列的片段和 (20 分)

給定一個正數數列&#xff0c;我們可以從中截取任意的連續的幾個數&#xff0c;稱為片段。例如&#xff0c;給定數列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我們有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目錄索引

序號內容1 【C Primer | 15】虛函數表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虛函數表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虛函數表剖析&#xff08;三&#xff09; 4一個C程序執行main函數前和執行完main函數后會發生什么。1 【C Priemr | 15】虛…

1046 劃拳 (15 分)

劃拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人劃拳的方法為&#xff1a;每人口中喊出一個數字&#xff0c;同時用手比劃出一個數字。如果誰比劃出的數字正好等于兩人喊出的數字之和&#xff0c;誰就贏了&#xff0c;輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪&…

多線程順序交替打印ABCD

題目&#xff1a;按照 ABCD的順序交替打印。 1. 測試代碼&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一個只出現一次的字符

在一個字符串(0<字符串長度<10000&#xff0c;全部由字母組成)中找到第一個只出現一次的字符,并返回它的位置, 如果沒有則返回 -1&#xff08;需要區分大小寫&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目錄

序號題目題解標記1 43. 字符串相乘 字符串2513. 找樹左下角的值二叉樹3 450. 刪除二叉搜索樹中的節點 二叉樹486. 分隔鏈表鏈表155 155. 最小棧 C題解棧77. 組合C題解回溯算法15.三數之和C題解

一個C++程序執行main函數前和執行完main函數后會發生什么。

總結&#xff1a; main函數執行之前&#xff0c;主要就是初始化系統相關資源&#xff1a; 設置棧指針初始化static靜態和global全局變量&#xff0c;即data段的內容將未初始化部分的賦初值&#xff1a;數值型short&#xff0c;int&#xff0c;long等為0&#xff0c;bool為FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10?5??). Then N integers are…

【面試寶典 | 01】面經

字節跳動提前批后端第三面涼經該來的終究會來的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人殺&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…

算法默寫

序號內容1快速排序算法2堆排序算法3歸并排序算法

1149 Dangerous Goods Packaging (25 分)

When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent &#xff08;氧化劑&#xff09; must not be packed with flamma…

《數據結構與算法》

序號內容1排序算法概念2快速排序算法3堆排序算法4歸并排序算法

一個類的指針指向NULL去訪問該類的成員函數

對象指針為NULL&#xff0c;為什么還是可以調用成員函數

1091 N-自守數 (15 分)

如果某個數 K 的平方乘以 N 以后&#xff0c;結果的末尾幾位數等于 K&#xff0c;那么就稱這個數為“N-自守數”。例如 392?2??25392&#xff0c;而 25392 的末尾兩位正好是 92&#xff0c;所以 92 是一個 3-自守數。 本題就請你編寫程序判斷一個給定的數字是否關于某個 N 是…