第21题
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> stack = new Stack();
int n = pushA.length;
for (int pushIndex=0,popIndex=0; pushIndex<n; pushIndex++){
stack.push(pushA[pushIndex]);
while (!stack.isEmpty() && popIndex<n && stack.peek()==popA[popIndex]){
stack.pop();
popIndex++;
}
}
return stack.isEmpty();
}
}
第22题
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> arrayList = new ArrayList<>();
if (root==null){
return arrayList; //这里不能返回null,类型不同
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
TreeNode t = queue.poll();
arrayList.add(t.val);
if (t.left != null){
queue.add(t.left);
}
if (t.right != null){
queue.add(t.right);
}
}
return arrayList;
}
}
第23题 二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if (sequence==null || sequence.length==0){
return false;
}
//判断是否满足左子树的值<=根节点的值<=右子树的值
return verify(sequence,0,sequence.length-1);
}
public boolean verify(int[] sequence, int first, int last){
if (last-first <= 1){
return true;
}
int rootVal = sequence[last];
int cutIndex = first;
//分出左右子树 且 左子树的值<=根节点的值
while(cutIndex<last && sequence[cutIndex]<=rootVal){
cutIndex++;
}
//右子树的值>=根节点的值
for (int i=cutIndex; i<last; i++){
if (sequence[i]<rootVal){
return false;
}
}
//左右子树各自递归
return verify(sequence,first,cutIndex-1) && verify(sequence,cutIndex,last-1);
}
}
第24题 二叉树中和为某一值的路径
输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
public class Solution {
ArrayList<ArrayList<Integer>> arrayList = new ArrayList();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
backTracking(root,target,new ArrayList<>());
return arrayList;
}
public void backTracking(TreeNode node, int target, ArrayList<Integer> path){
//递归的终止条件
if(node==null){
return;
}
path.add(node.val);
target-=node.val;
//是否符合路径要求
if(target==0 && node.left==null && node.right==null){
arrayList.add(new ArrayList<>(path));
}else{
//若不符合,先找左边,再找右边
backTracking(node.left,target,path);
backTracking(node.right,target,path);
}
//这个节点以下都不符合,把这个节点回退掉
path.remove(path.size()-1);
}
}
第25题
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。
public class Solution {
public RandomListNode Clone(RandomListNode pHead) {
if (pHead==null){
return null;
}
//1.插入新节点
RandomListNode current = pHead;
while (current!=null){
RandomListNode clone = new RandomListNode(current.label);//这里不能直接将current赋给clone,否则会连指针一起复制
clone.next = current.next;
current.next = clone;
current = clone.next;
}
//2.建立 random 链接
current = pHead;
while (current != null){
RandomListNode clone = current.next;
//这里做if判断的目的是防止没有randoom指针,null是内有next的
if (current.random != null){
clone.random = current.random.next;
}
current = clone.next;
}
//3.拆分
current = pHead;
RandomListNode pCloneHead = pHead.next;
while (current.next != null){
RandomListNode next = current.next;
current.next = next.next;
current = next;
}
return pCloneHead;
}
}