沃尔玛供应链
springboot 自动装配
2、dubbo超时组件
3、netty常用handler,粘包拆包
链路追踪怎么做的
4、docker file怎么写的
5、redis线程模型
6、redis怎么解决2个操作是事务
7、热点表新增字段超时怎么处理
8、灰度部署怎么切量
9、分布式事务,二段提交怎么实现的
如何得到二叉树的高度
import java.util.Deque;
import java.util.LinkedList;
public class TreeHigh {
public static void main(String[] args) {
TreeNode node = new TreeNode(1);
TreeNode node11 = new TreeNode(2);
TreeNode node12 = new TreeNode(3);
TreeNode node21 = new TreeNode(4);
TreeNode node22 = new TreeNode(5);
TreeNode node31 = new TreeNode(6);
node.left = node11;
node.right = node12;
node11.left = node21;
node11.right = node22;
node22.left = node31;
int high = getHigh(node);
System.out.println(high);
}
public static int getHigh(TreeNode node) {
if (node == null) {
return 0;
}
int high = 0;
Deque<TreeNode> stack = new LinkedList<>();
stack.push(node);
while (!stack.isEmpty()) {
int size = stack.size();
for (int i = 0; i < size; i++) {
TreeNode n = stack.pollLast();
if (n.left != null) {
stack.push(n.left);
}
if (n.right != null) {
stack.push(n.right);
}
}
high++;
}
return high;
}
public static class TreeNode {
public TreeNode left;
public TreeNode right;
public int value;
public TreeNode(int value) {
this.value = value;
}
}
}
2个数组,如何得到2个数组所有成员的中位数
public class ArraysMiddle {
public static void main(String[] args) {
int[] a = new int[]{2, 3, 7, 1, 3};
int[] b = new int[]{1, 32, 6, 5, 8};
//预期1,1,2,3,3,5,6,7,8,32取中位数,偶数取3+5/2,奇数个取中间数
//目标是取中位数,可以不用完全排序,可以考虑使用冒泡
System.out.println(getMuddle(a,b));
}
private static int getMuddle(int[] a,int[] b ){
int al = a.length;
int bl = b.length;
int l = al + bl;
int m = (l+1) / 2;
//只排序一半<m+1
for (int i = 0; i < m+1; i++) {
for (int j = 1; j < l-i ; j++) {
int temp;
if (j < al) {
if (a[j-1] > a[j]) {
temp = a[j-1];
a[j - 1] = a[j];
a[j] = temp;
}
}else if(j == al){
if (a[j-1] >b[j-al]) {
temp = a[j-1];
a[j - 1] = b[j-al];
b[j-al]= temp;
}
}else{
if (b[j-al-1] >b[j-al]) {
temp = b[j-al-1];
b[j-al-1] = b[j-al];
b[j-al]= temp;
}
}
}
}
//已排序一半
if (l % 2 == 1) {
if (m > al){
return b[m-al-1];
}else{
return a[m-1];
}
}else{
if (m < al){
return (a[m - 1] + a[m]) / 2;
}else if (m == al){
return (a[m - 1] + b[0]) / 2;
} else{
return (b[m - al-1] + b[m-al]) / 2;
}
}
}
}
标签:TreeNode,int,al,return,new,面实,public
From: https://www.cnblogs.com/wish5714/p/18176731