leetcode Minimum Depth of Binary Tree 1.11 难度系数1

来源:互联网 发布:python 爬虫工作原理 编辑:程序博客网 时间:2024/05/21 10:39

Question:

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int minDepth(TreeNode root) {         if(root==null) return 0;                    int l = minDepth(root.left);          int r = minDepth(root.right);                    if(l==0) return r+1;          if(r==0) return l+1;                    return Math.min(l, r) + 1;                }  }


0 0
原创粉丝点击