剑指offer面试题 java解答21-25

面试题21:包含min函数的栈

import java.util.Stack;
public class Test21<T extends Comparable<? super T>>{
    private Stack stack;
    private Stack min_stack;
    private T minTop=null;
    public Test21(){
        stack=new Stack();
        min_stack=new Stack();
    }
    public void push(T t){
        stack.push(t);
        if (min_stack.size()==0) 
        {
            min_stack.push(t);
            minTop=(T) t;
        }else 
        {
            if (minTop.compareTo(t)<0) 
            {
                min_stack.push(minTop);
            }else 
            {
                minTop=(T) t;
                min_stack.push(t);
            }
        }
    }
    public T pop(){
        if (stack.size()>0&&min_stack.size()>0) 
        {
            min_stack.pop();
            //获取栈顶元素 赋值给 minTop
            minTop=(T) min_stack.peek();
            return (T) stack.pop();
        }else {
            return null;
        }
    }
    public T min(){
        if (stack.size()>0&&min_stack.size()>0) 
        {
            return minTop;
        }else 
        {
            return null;
        }
    }
    public static void main(String[] args) {
        int[] a={3,4,2,1,};
        Test21 t=new Test21();
        for (int i = 0; i < a.length; i++) {
            t.push(a[i]);
            System.out.println(t.min());
        }
        t.pop();
        System.out.println(t.min());
        t.pop();
        System.out.println(t.min());
        t.push(0);
        System.out.println(t.min());
    }
}

面试题22:栈的压入、弹出序列

import java.util.Stack;
public class Test22<T>{
    public boolean IsPopOrder(T[] pPush,T[] pPop){
        if (pPop==null||pPush==null) {
            return false;
        }
        if (pPop!=null&&pPush!=null&&pPop.length!=pPush.length) {
            return false;
        }
        boolean isPossible=false;
        Stack stack=new Stack();
        int pushIndex=0;
        int popIndex=0;
        while (popIndex<pPush.length)
        {
            while (stack.isEmpty()||stack.peek()!=pPop[popIndex]) 
            {
                if (pushIndex==pPush.length) 
                {
                    break;
                }
                stack.push(pPush[pushIndex]);
                pushIndex++;
            }

            if (stack.peek()!=pPop[popIndex])
            {
                //压缩序列所有数字都已入栈,还没有找到对应弹出的数字
                break;
            }
            stack.pop();
            popIndex++;
        }
        if (stack.isEmpty()&&popIndex==pPush.length) 
        {
            isPossible=true;
        }else {
            isPossible=false;
        }
        return isPossible;
    }
    public static void main(String[] args) {
        Integer[] pPush={1,2,3,4,5};
        Integer[] pPop={4,5,3,2,1};
        Test22 t=new Test22();
        boolean isPopOrder=t.IsPopOrder(pPush, pPop);
        System.out.println(isPopOrder);
    }
}

面试题23:从上往下打印二叉树

import java.util.LinkedList;
public class Test23<T>{
    private class Node<T> {
        public T value;
        public Node<T> lChild;
        public Node<T> rChild;
        public Node(T value) {
            this.value = value;
        }
    }
    public Node<T> root=null;
    private int pos = 0;
    public void creatBiTree(T[] value){
        pos=0;
        root=creatBiTree(root, value);
    }
    // 以node为根节点,创建一棵树,返回此根节点
    private Node<T> creatBiTree(Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node<T>(t);
            node.lChild = creatBiTree(node.lChild, value);
            node.rChild = creatBiTree(node.rChild, value);
        }
        return node;
    }
    public void PrintFromTopToBottom(Node root) {
        LinkedList<Node> queue=new LinkedList<Node>();
        queue.add(root);
        while (!queue.isEmpty())
        {
            Node node=queue.poll();
            System.out.println(node.value);
            if (node.lChild!=null) 
            {
                queue.add(node.lChild);
            }
            if (node.rChild!=null) 
            {
                queue.add(node.rChild);
            }
        }
    }
    public static void main(String[] args) {
        Object[] a = { 8, 6, 5, null, null, 7, null, null, 10, 9, null, null, 11, null, null };
        Test23 t = new Test23();
        t.creatBiTree(a);
        t.PrintFromTopToBottom(t.root);
    }

}

面试题24:二叉搜索树的后序遍历序列

这里写代码片public class Test24<T>{
    public boolean VerifySequenceOfBST(int[] sequence){
        if (sequence==null||sequence.length<=0) 
        {
            return false;
        }
        int length=sequence.length;
        int root=sequence[length-1];
        int i;
        for (i = 0; i < length-1; i++) 
        {
            if (sequence[i]>root)
            {
                break;
            }
        }
        int j;
        for (j = i; j < length-1; j++) {
            if (sequence[j]<root)
            {
                return false;
            }
        }
        boolean left=true;
        if (i>0) 
        {
            int[] lSequence=new int[i];
            System.arraycopy(sequence, 0, lSequence, 0, i);
            left=VerifySequenceOfBST(lSequence);
        }
        boolean right=true;
        if (i<length-1) 
        {
            int[] rSequence=new int[length-i-1];
            System.arraycopy(sequence, i, rSequence, 0, length-i-1);
            right=VerifySequenceOfBST(rSequence);
        }
        return left&&right;
    }
    public static void main(String[] args) {
        int[] a = { 5,7,6,9,11,10,8 };
        Test24 t = new Test24();
        boolean isBST=t.VerifySequenceOfBST(a);
        System.out.println(isBST);
    }
}

面试题25:二叉树中和为某一值的路径

import java.util.Vector;
public class Test25<T>{
    private class Node<T> {
        public T value;
        public Node<T> lChild;
        public Node<T> rChild;
        public Node(T value) {
            this.value = value;
        }
    }
    public Node<T> root=null;
    private int pos = 0;
    public void creatBiTree(T[] value){
        pos=0;
        root=creatBiTree(root, value);
    }
    // 以node为根节点,创建一棵树,返回此根节点
    private Node<T> creatBiTree(Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node<T>(t);
            node.lChild = creatBiTree(node.lChild, value);
            node.rChild = creatBiTree(node.rChild, value);
        }
        return node;
    }
    public Vector<Node> paths=new Vector<Node>();
    private int sum=0;
    public void FindPath(Node root,int expectedSum){
        findPath(root, 0, expectedSum);
    }
    private void findPath(Node root,int now,int expectedSum){
        if (root!=null) {
            now+=(Integer)root.value;
            paths.add(root);
            if (now==expectedSum&&root.lChild==null&&root.rChild==null) {
                printResult();
            }           
            if (root.lChild!=null) {
                findPath(root.lChild, now, expectedSum);
            }
            if (root.rChild!=null) {
                findPath(root.rChild, now, expectedSum);
            }
        }
        paths.remove(paths.size()-1);//递归返回时删除路径上的结点  清空路径  为了输出多条符合条件
    }
    private void printResult() {
        System.out.println();
        for (Node node : paths) {
            System.out.print("->"+node.value);
        }
    }
    public static void main(String[] args) {
        Object[] a = { 10, 5, 4, null, null,7,null,null, 12, null, null};
        Test25 t = new Test25();
        t.creatBiTree(a);
        t.FindPath(t.root, 22);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值