微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift Weekly Contest 122]LeetCode987. 二叉树的垂序遍历 | Vertical Order Traversal of a Binary Tree

Given a binary tree,return the vertical order traversal of its nodes values.

For each node at position (X,Y),its left and right children respectively will be at positions (X-1,Y-1)and (X+1,Y-1).

Running a vertical line from X = -infinity to X = +infinity,whenever the vertical line touches some nodes,we report the values of the nodes in order from top to bottom (decreasing Y coordinates).

If two nodes have the same position,then the value of the node that is reported first is the value that is smaller.

Return an list of non-empty reports in order of X coordinate.  Every report will have a list of values of nodes.

Example 1:

分享图片

Input: [3,9,20,null,15,7]
Output: [[9],[3,15],[20],[7]] Explanation: Without loss of generality,we can assume the root node is at position (0,0): Then,the node with value 9 occurs at position (-1,-1); The nodes with values 3 and 15 occur at positions (0,0) and (0,-2); The node with value 20 occurs at position (1,-1); The node with value 7 occurs at position (2,-2). 

Example 2:

分享图片

Input: [1,2,3,4,5,6,7]
Output: [[4],[2],[1,6],[3],[7]] Explanation: The node with value 5 and the node with value 6 have the same position according to the given scheme. However,in the report "[1,6]",the node value of 5 comes first since 5 is smaller than 6.

Note:

  1. The tree will have between 1 and 1000 nodes.
  2. Each node‘s value will be between 0 and 1000.

给定二叉树,按垂序遍历返回其结点值。

对位于 (X,Y) 的每个结点而言,其左右子结点分别位于 (X-1,Y-1) 和 (X+1,Y-1)

把一条垂线从 X = -infinity 移动到 X = +infinity ,每当该垂线与结点接触时,我们按从上到下的顺序报告结点的值( Y 坐标递减)。

如果两个结点位置相同,则首先报告的结点值较小。

按 X 坐标顺序返回非空报告的列表。每个报告都有一个结点值列表。

示例 1:

分享图片

输入:[3,7]
输出:[[9],[7]]
解释: 
在不丧失其普遍性的情况下,我们可以假设根结点位于 (0,0):
然后,值为 9 的结点出现在 (-1,-1);
值为 3 和 15 的两个结点分别出现在 (0,0) 和 (0,-2);
值为 20 的结点出现在 (1,-1);
值为 7 的结点出现在 (2,-2)。

示例 2:

分享图片

输入:[1,7]
输出:[[4],[7]]
解释:
根据给定的方案,值为 5 和 6 的两个结点出现在同一位置。
然而,在报告 "[1,6]" 中,结点值 5 排在前面,因为 5 小于 6。

提示

  1. 树的结点数介于 1 和 1000 之间。
  2. 每个结点值介于 0 和 1000 之间。
Runtime: 20 ms
Memory Usage: 4.1 MB
 1 /**
 2  * DeFinition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var hi:[[Int]] = [[Int]]()
16     func verticalTraversal(_ root: TreeNode?) -> [[Int]] {
17         dfs(root,0,0)
18         hi.sort(by:sortArray)
19         var ret:[[Int]] = [[Int]]()
20         var i:Int = 0
21         while(i < hi.count)
22         {
23             var j:Int = i
24             while(j < hi.count && hi[j][1] == hi[i][1])
25             {
26                 j += 1
27             }
28             var item:[Int] = [Int]()
29             for k in i..<j
30             {
31                 item.append(hi[k][0])
32             }
33             ret.append(item)
34             i = j
35         }
36         return ret
37     }
38     
39     func sortArray(_ a:[Int],_ b:[Int]) -> Bool
40     {
41         if a[1] != b[1] {return a[1] < b[1]}
42         if a[2] != b[2] {return a[2] > b[2]}
43         return a[0] < b[0]
44     }
45     
46     func dfs(_ cur: TreeNode?,_ x:Int,_ y:Int)
47     {
48         if cur == nil {return}
49         hi.append([cur!.val,x,y])
50         dfs(cur!.left,x-1,y-1)
51         dfs(cur!.right,x+1,y-1)
52     }
53 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐