-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199-binary-tree-right-side-view.kt
35 lines (32 loc) · 1.16 KB
/
199-binary-tree-right-side-view.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun rightSideView(root: TreeNode?, depth: Int, list: MutableList<Int>) {
if(root == null) return
if(depth >= list.size) {
list.add(root.`val`)
}
if(root.right != null) rightSideView(root.right, depth + 1, list)
if(root.left != null) rightSideView(root.left, depth + 1, list)
}
fun rightSideView(root: TreeNode?): List<Int> {
/*
Level order traversal (favoring the right side) -> Take the right element
Postorder traversal -> Only print out/add the most deep element on the right of the layer
O(n) time, where n is the number of nodes within our tree
Depth is defined in this case as # edges between the node and the root
*/
if(root == null) return emptyList<Int>()
val rightSide = mutableListOf<Int>(root.`val`)
rightSideView(root, 0, rightSide)
return rightSide
}
}