LeetCode - The World's Leading Online Programming Learning Platform
Build a tree with all conditions and traverse it through DFS to find the answer.
Since the robot has only two possible moves for the next step, right or down, we can construct a binary tree.
Each node represents a grid between the robot and the endpoint. The left subtree indicates moving down, and the right subtree indicates moving right.
When a node is 1 * 1
, it means that the robot has reached the endpoint. If any value in a node is 0
, it means that the robot has gone out of bounds.
Here is the tree and its annotations.
1*1
, return 1
, indicating that there is 1 way to reach the destination.0
, return 0
, indicating that there is no way to reach the destination on this path.Return the sum of the values of the left subtree and the right subtree
We use hash map to cache nodes.