Introduction
It is an easy tree level algorithm called minimum depth of binary tree. I like to consider two ideas, one is to use breadth first search and then shortest path and then terminate search early. Another one is to use recursive function to do depth first search, to prune the search, if the current depth is bigger than minimum depth found so far, then current path should be terminated.
My practice
Here is my C# code. My first practice failed on one test case. My base case is wrong. I should consider the base case with a node not null, but left child and right child both are null. Null node should not be considered as a base case.
No comments:
Post a Comment