Knowee
Questions
Features
Study Tools

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

Question

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

🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem can be solved using a breadth-first search (BFS) algorithm. Here is a Python solution:

from collections import deque

class Solution:
    def findBottomLeftValue(self, root):
        queue = deque([(root, 0)])
        result = None
        max_depth = -1

        while queue:
            node, depth = queue.popleft()

            if node:
                if depth > max_depth:
                    max_depth = depth
                    result = node.val

                queue.append((node.left, depth + 1))
                queue.append((node.right, depth + 1))

        return result

This solution works by traversing the tree level by

This problem has been solved

Similar Questions

Given the root of a binary tree, invert the tree, and return its root. Example 1:Input: root = [4,2,7,1,3,6,9]Output: [4,7,2,9,6,3,1]

Invert the Binary TreeGiven the root of a binary tree, invert the tree, and return its root. Print the Inorder traversal of the inverted tree.Input format:The input consists of one line, space-separated elements denoting the nodes of the Binary Tree.Output format:Print the inorder traversal of the inverted tree in a single line.

Given the root of a binary tree, return the preorder traversal of its nodes' values. Example 1:Input: root = [1,null,2,3]Output: [1,2,3]Example 2:Input: root = []Output: []Example 3:Input: root = [1]Output: [1] Constraints:The number of nodes in the tree is in the range [0, 100].-100 <= Node.val <= 100 Follow up: Recursive solution is trivial, could you do it iteratively?

Given a Linked List Representation of Complete Binary Tree. Your task is to construct the Binary tree from the given linkedlist and return the root of the tree.The result will be judged by printing the level order traversal of the Binary tree. Note: The complete binary tree is represented as a linked list in a way where if the root node is stored at position i, its left, and right children are stored at position 2*i+1, and 2*i+2 respectively. H is the height of the tree and this space is used implicitly for the recursion stack.

Consider the below codetakeInput() print("Enter root data") rootData = int(input()) if (rootData == -1) return None root = BinaryTreeNode(rootData) root.left = takeInput() root.right = takeInput() return rootWhat will be the input(excluding -1) to above code to construct this tree ? 1) 2 7 2 6 5 11 5 9 4 2) 2 7 5 6 11 2 5 4 9 3) 2 7 5 2 6 9 5 11 4

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.