Last updated on October 5th, 2024 at 09:08 pm
Here, We see Find Bottom Left Tree Value LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.
List of all LeetCode Solution
Topics
Breadth-First Search, Depth-First Search, Tree
Companies
Microsoft
Level of Question
Medium
Find Bottom Left Tree Value LeetCode Solution
Table of Contents
Problem Statement
Given the root of a binary tree, return the leftmost value in the last row of the tree.
Example 1:
Input: root = [2,1,3]
Output: 1
Example 2:
Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
1. Find Bottom Left Tree Value Leetcode Solution C++
class Solution { public: int findBottomLeftValue(TreeNode* root) { queue<TreeNode*> q; q.push(root); int leftmost_value; while (!q.empty()) { TreeNode* node = q.front(); q.pop(); leftmost_value = node->val; if (node->right) { q.push(node->right); } if (node->left) { q.push(node->left); } } return leftmost_value; } };
2. Find Bottom Left Tree Value Solution Java
class Solution { public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(); q.offer(root); while(q.peek() != null) { root = q.poll(); if(root.right !=null) q.offer(root.right); if(root.left !=null) q.offer(root.left); } return root.val; } }
3. Find Bottom Left Tree Value Solution JavaScript
var findBottomLeftValue = function(root) { const queue = [root]; let leftmostValue; while (queue.length > 0) { const node = queue.shift(); leftmostValue = node.val; if (node.right) { queue.push(node.right); } if (node.left) { queue.push(node.left); } } return leftmostValue; };
4. Find Bottom Left Tree Value Solution Python
class Solution(object): def findBottomLeftValue(self, root): queue = deque([root]) leftmost_value = None while queue: node = queue.popleft() leftmost_value = node.val if node.right: queue.append(node.right) if node.left: queue.append(node.left) return leftmost_value