leetcode 二叉树的最小深度

来源:互联网 发布:正版bim软件多少钱 编辑:程序博客网 时间:2024/06/11 01:44

二叉树的最小深度定义: 根节点到叶子节点的最短距离。  叶子节点:左节点和右节点都为null。

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


0 0
原创粉丝点击