leetcode 广搜小题两道

版权声明:拥抱开源,欢迎转载,转载请保留原文链接~ https://blog.csdn.net/u010429424/article/details/82818841

leetcode 广搜小题两道

周末无聊,故刷两道小题,用的都是广搜的思想

  1. Minimum Depth of Binary Tree

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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.

求树的最小深度。思路是用广搜,按层遍历树,当遇到叶子节点时,返回当前的层数。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
     
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        int depth = 0;
     
        while(list.size() > 0) {
            int length = list.size();
            depth++;
            for(int i = 0; i < length; i++) {
                TreeNode node = list.removeFirst();
                if(node.left == null && node.right == null) {
                    return depth;
                }
                if(node.left != null) {
                    list.add(node.left);
                }
                if(node.right != null) {
                    list.add(node.right);
                }    
            }
        }
        return depth;
    }
}

  1. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example 1:

Input: 
    2
   / \
  2   5
     / \
    5   7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

Input: 
    2
   / \
  2   2
Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value.

这道题的二叉树有一个的特点:父节点的值,是左、右孩子的最小值。由此推断出:根节点是整棵树中的最小值,即树中第一小的值。要找到树中第二小的值,只需要找到那些比根节点值大的节点,并从中找出拥有最小值的节点。

依然用广搜的思想,按层遍历树,如果遇到一个节点的值大于根节点,说明这个节点的左、右子树中的所有节点的值,都大于根节点值,我们就不用遍历该节点的孩子们了。

比如,Example 1中,节点5大于树根2,则节点5的左、右孩子也都大于树根2。既然我们现在要找到树中第二小的值,那肯定不会是5的左右孩子了。第二小的值,应该是一个范围在**(2,5]**之间的值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int findSecondMinimumValue(TreeNode root) {
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        int min = Integer.MAX_VALUE;
        
        while(list.size() > 0) {
            TreeNode node = list.removeFirst();            
            if(node.val > root.val) {
                min = Math.min(min, node.val);
                continue;
            }
            if(node.left != null) {
                list.add(node.left);
            }
            if(node.right != null) {
                list.add(node.right);
            }            
        }
        return min == Integer.MAX_VALUE ? -1 : min;
    }
}

思考1:招不在多,管用就行,手撸广搜可以解决很多相似的问题,这样算不算是举一反三呢?

思考2:代码写的渣,不要笑话我,有没有更好的写法,想探究~

猜你喜欢

转载自blog.csdn.net/u010429424/article/details/82818841