算法程序题

如何知道二叉树的深度?

①递归实现:

为了求树的深度,可以先求其左子树的深度和右子树的深度,可以用递归实现,递归的出口就是节点为空。返回值为0;

public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root==null) return 0;
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return (left>right)?(left+1):(right+1);
    }
}

②非递归实现:

利用层次遍历的算法,将每一层的所有节点放入队列中,在将每一层节点的左右子节点存入放入到队列中,用一个变量记录树的高度即可。

import java.util.*;
public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root==null) 
            return 0;
        Queue<TreeNode> result = new LinkedList<>();
        result.add(root);
        int height = 0;
        while(!result.isEmpty()){
            //获取当前的根节点
            int size = result.size();
            while(size>0){//遍历当前层,将该层的子节点都加入队列中
                TreeNode nowroot = result.poll();
                if(nowroot.left!=null) 
                    result.add(nowroot.left);
                if(nowroot.right!=null) 
                    result.add(nowroot.right);
                size = size-1;//
            }
            height += 1;//高度加1
        }
        return height;
    }
}

链表反转

输入:

{1,2,3}

返回值:

{3,2,1}

递归法

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
         ListNode newHead=ReverseList(head.next);
        head.next.next=head;
        head.next=null;
        return newHead;

}

迭代法

public class Solution {
public ListNode ReverseList(ListNode head) {
 ListNode pre=null,next=null;
        while(head!=null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
}
}

排序

输入:

[5,2,3,1,4]

返回值:

[1,2,3,4,5]

选择排序

 public int[] MySort (int[] arr) {
       int min,temp,temp1;
            for(int i=0; i<arr.length-1; i++){
                min = arr[i];
                temp = i;
                for(int j=i+1;j<arr.length;j++){
                    if(arr[j]<min){
                        min=arr[j];
                        temp = j;
                    }
                }
                temp1 = arr[i];
                arr[i] =arr[temp];
                arr[temp] =temp1;
            }
return arr;
 }

冒泡排序

    public int[] MySort (int[] arr) {
     int temp;
      for(int i=0;i<arr.length-1;i++){
          for(int j=0;j<arr.length-1-i;j++){
              if(arr[j]>arr[j+1]){
                  temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;             }
          }
      }
        return arr;
    
    }

题目Given a string, compute recursively (no loops) the number of "22" substrings in the string. The "22" substrings should not overlap

count11("22abc22") -> 2
count11("abc22x22x22") -> 3
count11("222") -> 1

答案

 实现二叉树先序,中序和后序遍历 

 public int[][] threeOrders (TreeNode root) {
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        ArrayList<Integer> list3 = new ArrayList<>();
        
        preOrder(root,list1);
        inOrder(root,list2);
        postOrder(root,list3);
        int[][] arr = new int[3][list1.size()];
            for(int j=0;j<list1.size();j++){
                arr[0][j]=list1.get(j);
                arr[1][j]=list2.get(j);
                arr[2][j]=list3.get(j);
            }
        return arr;
    }
    
    public void preOrder(TreeNode root,ArrayList<Integer> list){
        if(root==null){
            return;
        }
        list.add(root.val);
        preOrder(root.left,list);
        preOrder(root.right,list);
    }
    
     public void inOrder(TreeNode root,ArrayList<Integer> list){
         if(root==null){
            return;
        }
        inOrder(root.left,list);
        list.add(root.val);
        inOrder(root.right,list);
    }
     public void postOrder(TreeNode root,ArrayList<Integer> list){
         if(root==null){
            return;
        }
       
        postOrder(root.left,list);
        postOrder(root.right,list);
         list.add(root.val);
    }

合并两个排序的链表

   public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null){
    return list2;
}
        if(list2 == null){
    return list1;
}
        if(list1.val<list2.val){
            list1.next = Merge(list1.next,list2);
            return list1;
        }
          else{
            list2.next = Merge(list2.next,list1);
            return list2;
        }
    }

用两个栈实现队列

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if(stack2.size()<=0){
            while(stack1.size()!=0){
                stack2.push(stack1.pop());
            }
        }
return stack2.pop();    
    }
}

跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

     if(target==1){
            return 1;
        }
        if(target==2){
            return 2;
        }
        return jumpFloor(target-1)+jumpFloor(target-2);

编程题:写一个函数,找到一个文件夹下所有文件,包括子文件夹

import java.io.File;
public class Counter2 {
    public static void main(String[] args) {
        //取得目标目录
        File file = new File("D:");
        //获取目录下子文件及子文件夹
        File[] files = file.listFiles();
        readfile(files);
    }

    public static void readfile(File[] files) {
        if(files == null) {// 如果目录为空,直接退出  
            return; 
        }
        for(File f:files) {
            //如果是文件,直接输出名字
            if(f.isFile()) {
                System.out.println(f.getName());
            }
            //如果是文件夹,递归调用
            else if(f.isDirectory()) {
                readfile(f.listFiles());
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值