题解 | 二叉树的最小深度

二叉树的最小深度

https://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724?tpId=46&tqId=29030&rp=1&difficulty=&judgeStatus=&tags=/question-ranking

import java.util.*;


// public class TreeNode {
//     int val = 0;
//     TreeNode left = null;
//     TreeNode right = null;
// }

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int run (TreeNode root) {
        ArrayList<Integer> path = new ArrayList<>();
        // write code here
        if (root == null) {
            return 0;
        }

        DFS(root, 1, path);
        return Collections.min(path);
    }

    public void DFS(TreeNode root, int depth, ArrayList<Integer> path) {
        if (root.left != null) {
            DFS(root.left, depth + 1, path);
        }
        if (root.right != null) {
            DFS(root.right, depth + 1, path);
        }
        if (root.left == null && root.right == null) {
            path.add(depth);
        }
    }
}

全部评论

相关推荐

求求要我吧:你教育经历放在下面干什么,而且27届还是28届啊()另外看你简历有两面,通常来说投递运营岗位一面简历就够了。另外个人总结要写也放在简历最下面,然后你奖项那里是2019年的哇哈哈,那你究竟投递的是社招还是实习?实习的话你是第几届是肯定要写出来的,社招的话你这个工作经历又太短太花了
点赞 评论 收藏
分享
02-11 14:29
已编辑
字节跳动_QA
Edgestr:这种的写代码最狠了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务