C# recursive solution


  • 0
    B
    public class Solution {
        public int MinDepth(TreeNode root) {
            if(root == null) {
                return 0;
            }
            int leftDepth = MinDepth(root.left);
            int rightDepth = MinDepth(root.right);
            
            if(rightDepth == 0) {
                return 1 + leftDepth;
            }
            else if(leftDepth == 0) {
                return 1 + rightDepth;
            }
            
            return  1 + Math.Min(leftDepth, rightDepth);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.