leetcode111(二叉树的最小深度 变题)

题目如下

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回它的最小深度  2.

这道题是二叉树的最大深度的变题,但是题目需要考虑的东西多了很多
我还是照例写了两种写法

1.递归解法

这里需要考虑的东西主要是使用递归返回最小层数,但是我卡了一会,一开始卡的思路是直接按照最大的深度来思考,但是如果按照最大深度的相反来思考的话,会自动忽略某个节点没有左子树或右子树但是却不是叶子节点的情况,考虑到这个情况一般就可以做出来了,代码如下

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root == None:
            return 0
        else:
            return self.again(root)
    def again(self, root):
        if root==None:
            return 0
        else:
            if root.left == None:
                return self.again(root.right)+1
            elif root.right == None:
                return self.again(root.left)+1
            else:
                return min(self.again(root.left),self.again(root.right))+1

2。使用栈来模拟DFS

这个方法时间复杂度比较高,但是实现起来比较简单。代码就是寻找每一个叶子节点,然后对比最小深度就可以了。
代码如下

import java.util.Stack;

// import javax.swing.tree.TreeNode;

// class Solution {
//     public int minDepth(TreeNode root) {
//         if(root == null)
//             return 0;
//         if(root.left != null && root.right != null)
//             return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
//         else if(root.left != null)
//             return minDepth(root.left) + 1;
//         else if(root.right != null)
//             return minDepth(root.right) + 1;
//         else
//             return 1;
//     }
// }
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
//栈实现一次DFS
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null)
        {
            return 0;
        }
        if (root.left == null && root.right == null)
        {
            return 1;
        }
        Stack<TreeNode> nodeStack = new Stack<>();
        Stack<Integer> numberStack = new Stack<>();
        nodeStack.push(root);
        numberStack.push(1);
        int min_depth = 10000;
        while (nodeStack.size() != 0)
        {
            TreeNode node = nodeStack.pop();
            int number = numberStack.pop();
            //每找到一个叶子节点,用该叶子节点的层数与min_depth的值对比,将小的换入。
            //这样就不用考虑不是叶子而导致的数值出错
            if (node.left == null && node.right == null)
            {
                min_depth = java.lang.Math.min(min_depth, number);
            }
            if (node.left != null)
            {
                nodeStack.push(node.left);
                numberStack.push(number+1);
                // min_depth = java.lang.Math.min(min_depth, number+1);
            }
            if (node.right != null)
            {
                nodeStack.push(node.right);
                numberStack.push(number+1);
                // min_depth = java.lang.Math.min(min_depth, number+1);
            }
        }
        return min_depth;
    }
}

由于我个人刷题一般是用python写一种解法,用Java写一种解法,所以写着写着语法可能有点混乱,写的python代码可能不够pythonic,大佬看到的话轻喷(wuwuwu

posted @ 2019-05-14 17:41  ayang818  阅读(117)  评论(0编辑  收藏  举报