2018-08-07 19:36:30 +08:00

53 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.raorao.leetcode;
/**
* 二叉树最短路径.
*
* 题目描述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.
*
* @author Xiong Raorao
* @since 2018-08-07-17:01
*/
public class Q1 {
public static void main(String[] args) {
}
/**
思路:
递归若为空树返回0
若左子树为空,则返回右子树的最小深度+1加1是因为要加上根这一层下同
若右子树为空,则返回左子树的最小深度+1
若左右子树均不为空,则取左、右子树最小深度的较小值,+1
* @param root
* @return
*/
public int run(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null) {
return run(root.right) + 1;
}
if (root.right == null) {
return run(root.left) + 1;
}
return run(root.left) < run(root.right) ? run(root.left) + 1 : run(root.right) + 1;
}
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}