Second Minimum Node In a Binary Tree
给一个很特殊的二叉树, 返回第二小的值. 直接都放到list里, 然后找到值
/**
* 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) {
List<Integer> list = new ArrayList<>();
bfs(root, list);
Collections.sort(list);
int first = -1;
int second = -1;
for(Integer i : list) {
if(first == -1){
first = i;
} else if(i != first) {
second = i;
break;
}
}
return second;
}
private void bfs(TreeNode root, List<Integer> list) {
if(root == null)
return;
list.add(root.val);
bfs(root.left, list);
bfs(root.right, list);
}
}