剑指offer题解21-25题解(Java)

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) {
        int len = pushA.length;
        if (pushA.length == 0 || popA.length == 0 || pushA.length != popA.length) {
          //push pop 为空 或者两数组不相等 
            return false;
          
        }
        Stack<Integer> stack = new Stack<Integer>();
        int j = 0;
        for (int i = 0; i < len; i++) {
            stack.push(pushA[i]);
            while (!stack.empty() && stack.peek() == popA[j]) {
              //栈顶元素与popA中相等,则弹出
                stack.pop();
                j++;
            }

        }
      //为空,则表示满足这个弹出顺序
        return stack.isEmpty();

    }
}

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

代码

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
 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>   ans=new ArrayList<Integer>();
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        if(root==null){
            //这里还不能返回null 
            return ans;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode temp=queue.poll();
            ans.add(temp.val);
            if(temp.left!=null){
                queue.offer(temp.left);
            }
            if(temp.right!=null){
                queue.offer(temp.right);
            }
        }
        return ans;
    }
}

23.二叉搜索树的后序遍历*

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

代码

二叉搜索树;左树所有小于根节点, 右树大于根节点

public class Solution {
    public boolean judge(int[] a, int l, int r) {
        if (l >= r) return true;
        int i = r;
        while (i > l && a[i - 1] > a[r]) --i;
        for (int j = i - 1; j >= l; --j) if (a[j] > a[r]) return false;
        return judge(a, l, i - 1) && (judge(a, i, r - 1));
    }

    public boolean VerifySquenceOfBST(int[] sequence) {
        if (sequence.length == 0) {
            return false;
        }
        return judge(sequence, 0, sequence.length - 1);

    }

}

24.二叉树中和为某个值的路径*

输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)

代码

题意是从根节点到任意节点的值和为target,不是任意节点的值和为target,一开始把题目想复杂了。

import javax.swing.tree.TreeNode;
import java.util.ArrayList;

/**
 * public class TreeNode {
 * int val = 0;
 * TreeNode left = null;
 * TreeNode right = null;
 * <p>
 * public TreeNode(int val) {
 * this.val = val;
 * <p>
 * }
 * <p>
 * }
 */
public class Solution {

    private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    private ArrayList<Integer> list = new ArrayList<Integer>();

    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {
        //dfs
        //root为空,说明已经遍历完,返回结果
        if (root == null) return result;
        list.add(root.val);
        target -= root.val;
        if (target == 0 && root.left == null && root.right == null)
            //表示本路径满足题意,加入到结果中
            result.add(new ArrayList<Integer>(list));
        
        FindPath(root.left, target);
        FindPath(root.right, target);
        //回溯
        list.remove(list.size() - 1);
        target += root.val;
        return result;
    }
}

25.复杂链表复制*

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

代码

不会,想的比较简单

参考

在这里插入图片描述


public class Solution {
    public RandomListNode Clone(RandomListNode pHead){
        if(pHead==null)
            return null;
        RandomListNode pCur = pHead;
        //复制next 如原来是A->B->C 变成A->A'->B->B'->C->C'
        while(pCur!=null){
            RandomListNode node = new RandomListNode(pCur.label);
            node.next = pCur.next;
            pCur.next = node;
            pCur = node.next;
        }
        pCur = pHead;
        //复制random pCur是原来链表的结点 pCur.next是复制pCur的结点
        while(pCur!=null){
            if(pCur.random!=null)
                pCur.next.random = pCur.random.next;
            pCur = pCur.next.next;
        }
        RandomListNode head = pHead.next;
        RandomListNode cur = head;
        pCur = pHead;
        //拆分链表
        while(pCur!=null){
            pCur.next = pCur.next.next;
            if(cur.next!=null)
                cur.next = cur.next.next;
            cur = cur.next;
            pCur = pCur.next;
        }
        return head;       
    }
}
发布了36 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42297075/article/details/104376658