Skip to main content
 首页 » 编程设计

Java面试剑指offer系列五

2022年07月19日154arxive

21.输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

import java.util.Stack; 
 
public class Solution { 
    public boolean IsPopOrder(int [] pushA,int [] popA) { 
        Stack s = new Stack(); 
        int i = 0,j = 0; 
        s.push(pushA[i++]); 
         
        if(pushA == null || popA == null){ 
            return false; 
        }else if(pushA.length == 0 || popA.length == 0){ 
            return false; 
        }else if(pushA.length != popA.length){ 
            return false; 
        }else{ 
            while(j <= popA.length - 1){ 
                while(popA[j] != s.peek()){ 
                    if(i == pushA.length){ 
                        return false; 
                    } 
                    s.push(pushA[i++]); 
                } 
                j++; 
                s.pop(); 
            } 
        } 
         
        return true; 
    } 
}

22.从上往下打印出二叉树的每个节点,同层节点从左至右打印。

import java.util.ArrayList; 
import java.util.Queue; 
import java.util.LinkedList; 
/** 
public class TreeNode { 
    int val = 0; 
    TreeNode left = null; 
    TreeNode right = null; 
 
    public TreeNode(int val) { 
        this.val = val; 
 
    } 
 
} 
*/ 
public class Solution { 
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { 
        ArrayList<Integer> treeList = new ArrayList<Integer>();
     //这里要注意的是,Queue是一个接口,不能直接创建Queue的实例,只能创建实现Queue接口的子类 Queue
<TreeNode> q = new LinkedList<TreeNode>(); q.offer(root); if(root == null){ return treeList; } while(!q.isEmpty()){ TreeNode temp = q.poll(); treeList.add(temp.val); if(temp.left != null) q.offer(temp.left); if(temp.right != null) q.offer(temp.right); } return treeList; } }

23.输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

import java.util.Arrays; 
 
public class Solution { 
    public boolean VerifySquenceOfBST(int [] sequence) { 
        int[] leftTree; 
        int[] rightTree; 
         
        int len = sequence.length; 
        if(sequence==null || len == 0){ 
            return false; 
        } 
        int root = sequence[len-1]; 
        int i = 0; 
        for(; i < len-1; i++){ 
            if(sequence[i] > root) 
                break; 
        } 
         
        for(int j = i; j < len-1; j++){ 
            if(sequence[j] < root) 
                return false; 
        } 
         
        boolean left=true; 
        boolean right=true; 
         
        if(i>0){ 
            left=VerifySquenceOfBST(Arrays.copyOfRange(sequence, 0, i)); 
        } 
         
        if(i<len-1) 
            right=VerifySquenceOfBST(Arrays.copyOfRange(sequence, i, len-1)); 
        return (left&&right); 
 
    }

24.输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

import java.util.ArrayList; 
import java.util.Stack; 
/** 
public class TreeNode { 
    int val = 0; 
    TreeNode left = null; 
    TreeNode right = null; 
 
    public TreeNode(int val) { 
        this.val = val; 
 
    } 
 
} 
*/ 
public class Solution { 
    public ArrayList<Integer> s=new ArrayList<Integer>(); 
    private ArrayList<ArrayList<Integer>> allList=new ArrayList<ArrayList<Integer>>(); 
    private int sum=0; 
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) { 
        if(root==null){ 
            return allList; 
        } 
        s.add(root.val); 
        sum=sum+root.val; 
        if(root.left==null&&root.right==null){ 
            ArrayList<Integer> list=new ArrayList<Integer>(); 
            if(sum==target){ 
                for(int i=0;i<s.size();i++){ 
                    list.add(s.get(i)); 
                } 
                allList.add(list); 
            } 
        }else{ 
            if(root.left!=null){ 
                FindPath(root.left,target); 
            } 
            if(root.right!=null){ 
                FindPath(root.right,target); 
            } 
        } 
        sum=sum-s.get(s.size()-1); 
        s.remove(s.size()-1); 
        return allList; 
    } 
}

25.输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

/* 
public class RandomListNode { 
    int label; 
    RandomListNode next = null; 
    RandomListNode random = null; 
 
    RandomListNode(int label) { 
        this.label = label; 
    } 
} 
*/ 
public class Solution { 
    public RandomListNode Clone(RandomListNode pHead) 
    { 
        if(pHead == null) {  
            return null ; 
        } 
  
        RandomListNode head = new RandomListNode(pHead.label) ; 
        RandomListNode temp = head ; 
  
        while(pHead.next != null) { 
            temp.next = new RandomListNode(pHead.next.label) ; 
            if(pHead.random != null) { 
                temp.random = new RandomListNode(pHead.random.label) ; 
            } 
            pHead = pHead.next ; 
            temp = temp.next ; 
        } 
  
        return head ; 
    } 
}

本文参考链接:https://www.cnblogs.com/wgl1995/p/5790866.html