编程测试题 代码题 二叉树的四种遍历,非递归实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值