LeetCode 742: Closest Leaf in a Binary Tree Solution

Master LeetCode problem 742 (Closest Leaf in a Binary Tree), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

742. Closest Leaf in a Binary Tree

Problem Explanation

Explanation

To solve this problem, we can perform a two-step process:

  1. First, we need to construct a graph representation of the binary tree where each node is connected to its parent and children nodes.
  2. Then, we can perform a depth-first search (DFS) starting from the target node to find the closest leaf node.

Algorithmic Steps:

  1. Construct a graph G from the binary tree.
  2. Perform a DFS from the target node to find the closest leaf node.
  3. During the DFS, keep track of the distance from the current node to the closest leaf node found so far.
  4. Once we reach a leaf node, update the answer if the distance is smaller than the previously found closest leaf.
  5. Return the closest leaf node found.

Time Complexity:

  • Constructing the graph: O(N)
  • Performing DFS: O(N) Overall time complexity: O(N)

Space Complexity:

  • Graph representation: O(N)
  • Recursive stack space for DFS: O(N) Overall space complexity: O(N)

Solution Code

class Solution {
    public int findClosestLeaf(TreeNode root, int k) {
        Map<TreeNode, List<TreeNode>> graph = new HashMap<>();
        buildGraph(root, null, graph);

        Queue<TreeNode> queue = new LinkedList<>();
        Set<TreeNode> visited = new HashSet<>();

        for (TreeNode node : graph.keySet()) {
            if (node != null && node.val == k) {
                queue.offer(node);
                visited.add(node);
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode curr = queue.poll();
            if (curr.left == null && curr.right == null) {
                return curr.val;
            }
            for (TreeNode neighbor : graph.get(curr)) {
                if (!visited.contains(neighbor)) {
                    visited.add(neighbor);
                    queue.offer(neighbor);
                }
            }
        }
        return -1;
    }

    private void buildGraph(TreeNode node, TreeNode parent, Map<TreeNode, List<TreeNode>> graph) {
        if (node == null) return;
        if (!graph.containsKey(node)) {
            graph.put(node, new ArrayList<>());
            if (parent != null) {
                graph.get(node).add(parent);
                graph.get(parent).add(node);
            }
            buildGraph(node.left, node, graph);
            buildGraph(node.right, node, graph);
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 742 (Closest Leaf in a Binary Tree)?

This page provides optimized solutions for LeetCode problem 742 (Closest Leaf in a Binary Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 742 (Closest Leaf in a Binary Tree)?

The time complexity for LeetCode 742 (Closest Leaf in a Binary Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 742 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 742 in Java, C++, or Python.

Back to LeetCode Solutions