Performing a BFS on all directions in a Binary Tree
I've tried this problem before pre-pandemic and failed to realize that it requires something not very common: to perform a BFS on a binary tree across all directions. The "across all directions" means that it is necessary to create the link node -> parent. This can be done using a simple DFS storing the info in a hash table. Then once you find the node related to K (another DFS), then perform a BFS going left, right, parent. All the code together should run in linear time, with a constant 3 (O(3N)). Code is below, cheers, ACC. Closest Leaf in a Binary Tree - LeetCode Given the root of a binary tree where every node has a unique value and a target integer k , return the value of the nearest leaf node to the target k in the tree . Nearest to a leaf means the least number of edges traveled on the binary tree to reach any leaf of the tree. Also, a node is called a leaf if it has no children. Example 1: Input: root ...