镜像二叉树_汽水瓶

镜像二叉树

镜像二叉树s

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

汽水瓶

汽水瓶

image-20220503234824759

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]));
   }
 }
}

image-20220504090149263

全部评论

相关推荐

03-03 23:12
已编辑
北京邮电大学 Java
书海为家:我来给一点点小建议,因为毕竟还在学校不像工作几年的老鸟有丰富的项目经验,面试官在面试在校生的时候更关注咱们同学的做事逻辑和思路,所以最好在简历中描述下自己做过项目的完整过程,比如需求怎么来的,你对需求的解读,你想到的解决办法,遇到困难如何找人求助,最终项目做成了什么程度,你从中收获了哪些技能,你有什么感悟。
你的简历改到第几版了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务