leetcode(1)

来源:互联网 发布:wps表格查找重复数据 编辑:程序博客网 时间:2024/04/29 06:08

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.

public class Solution {    public int run(TreeNode root) {       return getDepth(root,0);    }    public  static int getDepth(TreeNode node,int height){        if(node==null){            return height;        }        if(node.left==null){            return getDepth(node.right,height+1);        }        if(node.right==null){            return getDepth(node.left,height+1);        }        return Math.min(getDepth(node.left,height+1),getDepth(node.right,height+1));    }}


原创粉丝点击