镜像二叉树_汽水瓶
镜像二叉树
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot==null){//根为空不做处理!
return null;
}
//单只树直接返回!
if(pRoot.left==null && pRoot.right==null)
return pRoot;
//处理根节点,交换左右节点
TreeNode temp=pRoot.left;
pRoot.left=pRoot.right;
pRoot.right=temp;
//相同方法处理左右子树!
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
} 汽水瓶
import java.util.*;
public class Main{
public static int fun(int x){
int result = 0;//保存结果数(换的饮料数)
int ret = 0; //保存每次没换的空瓶数(余数)
while(x>=3){
ret = x%3;
result += x/3;
//每次换完饮料 得到的 饮料 + 没换的空瓶 又是新的空瓶数
x = x/3 + ret;
}
//如果两个瓶子可以向老板借!
if(x==2){
result++;
}
return result;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] array = new int[10];
int i = 0;
while(sc.hasNext()){
int ret = sc.nextInt();
if(ret==0){
break;
}
array[i++] = ret;
}
for(int j=0;j<i;j++){
System.out.println(fun(array[j]));
}
}
}

查看10道真题和解析