博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编程测试题 代码题 二叉树的四种遍历,非递归实现
阅读量:3731 次
发布时间:2019-05-22

本文共 3037 字,大约阅读时间需要 10 分钟。

2019阿里巴巴编程测试题(在线和手写,记事本写)~~

要求:

给定一个字符串,按照给定的字节数要求输出,

如: str="ABCDEF", 输出4个字节,则为: ABCD. 

如: str="哈ABCDEF",输出4个字节,则应为 “哈AB”。

 我写的代码是这样的:

@Test

    public void test1() {
        String str="哈abcdf";
        System.out.println(outputByByteNum(str,4));
    }
    /**
     * @Title: outputByByteNum   
     * @Description: 按字节要求输出字符串 
     * @param: @param str 要求的字符串
     * @param: @param num 要求输出的字节数
     * @return: String
     */
    public static String outputByByteNum(String str ,int num){
        char[] ch=str.toCharArray();
        int count=0;
        StringBuilder sb=new StringBuilder();
        for(char c:ch){
            if((int)c>255 && count<num)
                count+=2;
            else if((int)c<255 && count<num)
                count+=1;
            else 
                break;
            sb.append(c);
        }
        return sb.toString();
    } 

希望各位大佬,给一种另外的判断字节数的方式。 谢谢。我这个是根据asc码来判断的。有点牵强

 

------

用非递归实现一个树的前序遍历;

public class Node {

    private int num;
    private Node left;
    private Node right;
    public Node Node(){
        Node J = new Node(8, null, null);  
        Node H = new Node(4, null, null);  
        Node G = new Node(2, null, null);  
        Node F = new Node(7, null, J);  
        Node E = new Node(5, H, null);  
        Node D = new Node(1, null, G);  
        Node C = new Node(9, F, null);  
        Node B = new Node(3, D, E);  
        Node A = new Node(6, B, C); 
        return A;
    }
    public Node(){}
    public Node(int num, Node left, Node right) {
        super();
        this.num = num;
        this.left = left;
        this.right = right;
    }

    public int getNum() {

        return num;
    }
    public Node getLeft() {
        return left;
    }
    public Node getRight() {
        return right;
    }
    @Override
    public String toString() {
        return "Node [num=" + num + ", left=" + left + ", right=" + right + "]";
    }
}

 

public class Main {

    public static void main(String[] args) {
        Node n=new Node().Node();
        posOrder1(n);
    }
    
  

//递归前

public static void preFirst(Node A){

  if(A!=null){

      System.out.println(A);

      preFirst(A.getLeft());

     preFirst(A.getRight());

  }

}

//递归中

public static void midorder(Node A){

    if(A!=null){

        midorder(A.getLeft());

        System.out.println(A);

        midorder(A.getRight());

    }

}

 

//递归后

public static void posOrder(Node A){

    if(A!=null){

        posOrder(A.getLeft());

        posOrder(A.getRight());

        System.out.println(A);

     }

}

 

    

    //非递归的前序遍历
    public static void preOrder1(Node Node)
    {
        Stack<Node> stack = new Stack<Node>();
        while(Node != null || !stack.empty())
        {
            while(Node != null)
            {
                System.out.println(Node + "   ");
                stack.push(Node);
                Node = Node.getLeft();
            }
            if(!stack.empty())
            {
                Node = stack.pop();
                Node = Node.getRight();
            }
        }
    }
    
    //非递归的中序遍历
    public static void midOrder1(Node Node)
    {
        Stack<Node> stack = new Stack<Node>();
        while(Node != null || !stack.empty())
        {
            while(Node != null)
            {
                stack.push(Node);
                Node = Node.getLeft();
            }
            if(!stack.empty())
            {
                Node = stack.pop();
                System.out.println(Node + "   ");
                Node = Node.getRight();
            }
        }
    }
    
    //非递归的后序遍历
    public static void posOrder1(Node Node)
    {
        Stack<Node> stack1 = new Stack<Node>();
        Stack<Integer> stack2 = new Stack<>();
        int i = 1;
        while(Node != null || !stack1.empty())
        {
            while (Node != null)
            {
                stack1.push(Node);
                stack2.push(0);
                Node = Node.getLeft();
            }

            while(!stack1.empty() && stack2.peek() == i)

            {
                stack2.pop();
                System.out.println(stack1.pop() + "   ");
            }

            if(!stack1.empty())

            {
                stack2.pop();
                stack2.push(1);
                Node = stack1.peek();
                Node = Node.getRight();
            }
        }
    }
}

转载地址:http://wkkin.baihongyu.com/

你可能感兴趣的文章
ETL工具Sqoop的入门学习(二)之eval语句使用以及import的增量导入。
查看>>
Java实现AES数据对称加密和解密算法!
查看>>
Python可视化各省近20年地区生产总值数据
查看>>
Python pandas库的Series与DataFrame常用命令详细讲解!
查看>>
python numpy 实现与(and),非与(not),或(or),异或(xor)逻辑运算!
查看>>
Java实现二叉查找树的前中后序遍历
查看>>
Springboot整合mybatis笔记
查看>>
Springboot整合mybatis访问数据并将读取的数据发送到前端通过echarts绘图展示。
查看>>
Springboot开发一个简单的可视化系统的总结!
查看>>
java实现数据元素间的排序之冒泡排序算法。
查看>>
java数组实现针对一个有序的数组插入一个数据并保持数组有序。
查看>>
java实现根据名单进行随机的小组分组。
查看>>
Java实现简单的数字拆分。题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
查看>>
Java 单链表的指定添加、删除数据的详细实现
查看>>
Java 双链表的指定添加、删除数据的详细实现
查看>>
Java双链表实现左旋转字符串
查看>>
Java Swing Jtable组件展示从MySQL数据库中获取到的的数据,可点击按钮查询数据并展示。
查看>>
电脑组合键无法调节亮度及其解决方法
查看>>
内网安全-ew工具使用之端口转发
查看>>
SpringMVC实现原理和SpringBootMVC实现原理
查看>>