21.栈的压入、弹出序列
题目描述:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
import java.util.*;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
for (int i = popA.length - 1; i >=0 ; i--) {
stack2.push(popA[i]);
}
for (int j = 0; j < pushA.length; j++){
stack1.push(pushA[j]);
while (!stack1.isEmpty() && !stack2.isEmpty() && stack1.peek().equals(stack2.peek())) {
stack1.pop();
stack2.pop();
}
}
if (stack1.isEmpty()) {
return true;
}
return false;
}
}
22.从上往下打印二叉树
题目描述:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
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<Integer> intList=new ArrayList<Integer>();
ArrayList<TreeNode> treeList=new ArrayList<TreeNode>();
if(root==null){
return intList;
}
treeList.add(root);
for(int i=0;i<treeList.size();i++){
TreeNode node= treeList.get(i);
if(node.left!=null){
treeList.add(node.left);
}
if(node.right!=null){
treeList.add(node.right);
}
intList.add(node.val);
}
return intList;
}
}
23.二叉搜索树的二叉遍历序列
题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
import java.util.*;
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence==null||sequence.length<=0){
return false;
}
int len=sequence.length;
int root=sequence[len-1];
int i=0;
for(;i<len-1;i++){
if(root<=sequence[i])
break;
}
int j=i;
for(;j<len-1;j++){
if(root>sequence[j]){
return false;
}
}
boolean leftFlag=true;
if (i>0) {
leftFlag=VerifySquenceOfBST(Arrays.copyOfRange(sequence,0,i));
}
boolean rightFlag=true;
if (i<len-1) {
rightFlag=VerifySquenceOfBST(Arrays.copyOfRange(sequence,i,sequence.length-1));
}
return leftFlag&&rightFlag;
}
}
24.二叉树中和为某一值的路径
题目描述:输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
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 {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> arr = new ArrayList<Integer>();
int num = 0;
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null){
return result;
}
boolean isLeaf = root.left==null&&root.right==null;
num+=root.val;
arr.add(root.val);
if(num==target&&isLeaf){
ArrayList<Integer> path = new ArrayList<Integer>();
for(int i=0;i<arr.size();i++){
path.add(arr.get(i));
}
result.add(path);
}
if(num<target&&root.left!=null){
FindPath(root.left,target);
}
if(num<target&&root.right!=null){
FindPath(root.right,target);
}
num-=root.val;
arr.remove(arr.size()-1);
return result;
}
}
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)
{
CloneNodes(pHead);
ConnectRandomNodes(pHead);
return ReConnectNodes(pHead);
}
// 第一步:根据原始链表的每个结点N创建对应的结点N'
private void CloneNodes(RandomListNode pHead)
{
RandomListNode node = pHead;
while (node != null)
{
RandomListNode cloneNode = new RandomListNode(node.label);
cloneNode.next = node.next;
cloneNode.random = null;
node.next = cloneNode;
node = cloneNode.next;
}
}
// 第二步:设置复制出来结点的random.假设原始结点的随机指向S,复制出来结点的random指向S后一个
private void ConnectRandomNodes(RandomListNode pHead)
{
RandomListNode node = pHead;
while (node != null)
{
RandomListNode clone = node.next;
if (node.random != null)
{
clone.random = node.random.next;
}
node = clone.next;
}
}
// 第三步:把这个长链表拆分成两个链表,奇数位置连接起来就是原始链表,偶数结点连接起来就是复制结点
private RandomListNode ReConnectNodes(RandomListNode pHead)
{
RandomListNode node = pHead;
RandomListNode cloneHead = null;
RandomListNode cloneNode = null;
// 设置第一个节点
if (node != null)
{
cloneHead = node.next;
cloneNode = node.next;
node.next = cloneNode.next;
node = node.next;
}
while (node != null)
{
cloneNode.next = node.next;
cloneNode = cloneNode.next;
node.next = cloneNode.next;
node = node.next;
}
return cloneHead;
}
}