21.棧的壓入、彈出序列
輸入兩個(gè)整數(shù)序列,第一個(gè)序列表示棧的壓入順序,請(qǐng)判斷第二個(gè)序列是否為該棧的彈出順序。假設(shè)壓入棧的所有數(shù)字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對(duì)應(yīng)的一個(gè)彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。
import java.util.ArrayList;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA==null || popA==null){
return false;
}else if(pushA.length==0 ||popA.length==0){
return false;
}
ArrayList<Integer> list =new ArrayList<Integer>();
int push = pushA[0];
int pop = popA[0];
int i=0,j=0;
while(true){
if(push==pop){
//優(yōu)先從隊(duì)列中取出一個(gè)
if(list.size()!=0){
push=list.remove(list.size()-1);
}else{
i++;//如果隊(duì)列里面沒有,就從后面拿一個(gè)
if(i==pushA.length){
i=i-1;
break;
}
push=pushA[i];
}
j++;
if(j==popA.length){
break;
}
pop=popA[j];
}else{
list.add(push);
i++;
if(i==pushA.length){
break;
}
push=pushA[i];
}
}
if(j==popA.length-1&&i==pushA.length-1){
if(list.size()==0){
return true;
}else{
return false;
}
}
return false;
}
}
22.從上往下打印二叉樹
從上往下打印出二叉樹的每個(gè)節(jié)點(diǎn),同層節(jié)點(diǎn)從左至右打印。
import java.util.ArrayList;
/**
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<TreeNode> list = new ArrayList<TreeNode>();
ArrayList<Integer> res = new ArrayList<Integer>();
if(root!=null){
list.add(root);
}
while(!list.isEmpty()){
TreeNode tmp = list.remove(0);
TreeNode left =tmp.left;
TreeNode right = tmp.right;
res.add(tmp.val);
if(left!=null){
list.add(left);
}
if(right!=null){
list.add(right);
}
}
return res;
}
}
23.二叉搜索樹的后序遍歷序列
輸入一個(gè)整數(shù)數(shù)組,判斷該數(shù)組是不是某二叉搜索樹的后序遍歷的結(jié)果。如果是則輸出Yes,否則輸出No。假設(shè)輸入的數(shù)組的任意兩個(gè)數(shù)字都互不相同。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence==null || sequence.length==0){
return false;
}
return check(sequence,0,sequence.length-1);
}
public boolean check(int[] array,int start,int end){
if(start>=end){
return true;
}
int size = end -start ;//最后一個(gè)不要跟自己比,去掉最后一個(gè)
int endVal =array[end];
//找出第一個(gè)比endVal大的位置
int mark=-1;
for(int i =start;i< size;i++){
if(array[i]>endVal){
mark=i;
break;
}
}
if(mark==-1){//都比endVal小
if(check(array,start,end-1)){
return true;
}
}else{
//mark之后的數(shù)都要比val大
for(int i=mark;i<size;i++){
if(array[i]<endVal){
return false;
}
}
if(check(array,start,mark-1) && check(array,mark,end-1)){
return true;
}
}
return false;
}
}
24.二叉樹中和為某一值的路徑
輸入一顆二叉樹和一個(gè)整數(shù),打印出二叉樹中結(jié)點(diǎn)值的和為輸入整數(shù)的所有路徑。路徑定義為從樹的根結(jié)點(diǎn)開始往下一直到葉結(jié)點(diǎn)所經(jīng)過(guò)的結(jié)點(diǎn)形成一條路徑。
import java.util.ArrayList;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
ArrayList<ArrayList<Integer>> res =travel(new ArrayList<Integer>(),target,root);
return res;
}
public ArrayList<ArrayList<Integer>> travel(ArrayList<Integer> list,int value,TreeNode root){
//為空的情況
ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
if(root==null){
return res;
}
ArrayList<Integer> newList =new ArrayList<Integer>();
//將list中的內(nèi)容復(fù)制到newlist中
for(Integer item:list){
newList.add(item);
}
int val = root.val;
newList.add(val);
int rest = value-val;
//葉子節(jié)點(diǎn)
if(root.left==null && root.right==null){
if(rest==0){
res.add(newList);
}else{
return res;
}
}else{//不是葉子節(jié)點(diǎn)
ArrayList<ArrayList<Integer>> left = travel(newList,rest,root.left);
ArrayList<ArrayList<Integer>> right = travel(newList,rest,root.right);
//合并兩項(xiàng),并返回
if(left!=null){
for(ArrayList<Integer> item:left){
res.add(item);
}
}
if(right!=null){
for(ArrayList<Integer> item:right){
res.add(item);
}
}
}
return res;
}
}
25.復(fù)雜鏈表的復(fù)制
輸入一個(gè)復(fù)雜鏈表(每個(gè)節(jié)點(diǎn)中有節(jié)點(diǎn)值,以及兩個(gè)指針,一個(gè)指向下一個(gè)節(jié)點(diǎn),另一個(gè)特殊指針指向任意一個(gè)節(jié)點(diǎn))。
import java.util.ArrayList;
/*
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;
}
//存儲(chǔ)地址
ArrayList<String> addresses=new ArrayList<String>();
addresses.add(pHead.toString());
//記錄頭指針
RandomListNode first = pHead;
//記錄頭指針
RandomListNode newOne = new RandomListNode(pHead.label);
//存儲(chǔ)指針
ArrayList<RandomListNode> res = new ArrayList<RandomListNode>();
res.add(newOne);
RandomListNode entity =newOne;
//int size=1;
while(pHead.next!=null){
pHead=pHead.next;
//獲取當(dāng)前地址
String address =pHead.toString();
//if(addresses.contains(address)){//已經(jīng)存在,回路
// entity.next = res.get(addresses.indexOf(address));//獲取
// break;
//}
//size++;
entity.next = new RandomListNode(pHead.label);
entity = entity.next;
res.add(entity);
addresses.add(pHead.toString());//默認(rèn)是地址
}
pHead = first;
if(pHead.random!=null){
newOne.random = res.get(addresses.indexOf(pHead.random.toString()));
}
entity=newOne;
//int i=0;
while(pHead.next!=null /*&& i<size*/){
pHead=pHead.next;
entity=entity.next;
if(pHead.random!=null){
entity.random = res.get(addresses.indexOf(pHead.random.toString()));
}
//i++;
}
return newOne;
}
}