leetcode 广搜小题两道

leetcode 广搜小题两道

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

111. 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;
    }
}

112. 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;
    }
}

199. Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:

1 <—
/ \
2 3 <—
 \    \
 5     4 <—

题目大意:
右视图下的二叉树,从右边放眼望去,输出看到的节点

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        if(root == null) {
            return result;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(root);
        while(!list.isEmpty()) {
            int range = list.size();
            for(int i = 0; i < range; i++) {
                TreeNode node = list.removeFirst();
                if(node.left != null) {
                    list.add(node.left);
                }
                if(node.right != null) {
                    list.add(node.right);
                }
                // 把最右边的节点加入到result中
                if(i == range - 1) {
                    result.add(node.val);
                }
            }
        }
        return result;
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值