Created
May 5, 2022 19:52
-
-
Save Turskyi/87719adb29beaeab5d807ffe529a72dd to your computer and use it in GitHub Desktop.
Given a binary tree, return the bottom-left most value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun findBottomLeftValue(root: TreeNode?): Int { | |
val queue: Queue<TreeNode?> = LinkedList<TreeNode?>() | |
queue.offer(root) | |
var node: TreeNode? = queue.peek() | |
while (queue.isNotEmpty()) { | |
node = queue.poll() | |
if (node?.right != null) { | |
queue.add(node.right) | |
} | |
if (node?.left != null) { | |
queue.add(node.left) | |
} | |
} | |
return node?.`val` ?: -1 | |
} | |
/* | |
Given the root of a binary tree, return the leftmost value in the last row of the tree. | |
Ex: Given the following binary tree… | |
1 | |
/ \ | |
2 3 | |
/ | |
4 | |
return 4. | |
Ex: Given the following binary tree… | |
8 | |
/ \ | |
1 4 | |
/ | |
2 | |
return 2. | |
* */ | |
/** | |
* @see [https://leetcode.com/problems/find-bottom-left-tree-value/](https://leetcode.com/problems/find-bottom-left-tree-value/) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment