Given a binary tree where every node has a unique value, and a target key k.
Find the value of the nearest leaf node to target k in the tree. If there are multiple cases, you should follow these priorities:
- The leaf node is in the left subtree of the node with k;
- The leaf node is in the right subtree of the node with k;
- The leaf node is not in the subtree of the node with k.
Note
- root represents a binary tree with at least 1 node and at most 1000 nodes.
- Every node has a unique node.val in range [1, 1000].
- There exists a node in the given binary tree for which node.val == k.
Example
No.1
Input: {1, 3, 2}, k = 1
Output: 3
Explanation:
1 | 1 |
No.2
Input: {1}, k = 1
Output: 1
Code
1 | public class TreeNode { |
1 | private TreeNode start; |