Problem 1:
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
public class Solution {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0)
return false;
return isPostorderTraversal(sequence, 0, sequence.length - 1);
}
public boolean isPostorderTraversal(int[] seq, int begin, int end){
if(begin >= end)
return true;
int root = seq[end];
int i = begin;
for(; i < end - 1; i++){ // find index i that separates left subtree from right subtree
if(seq[i] > root){
break;
}
}
for(int j = i; j < end -1; j++){ // judge if there exists an element's value greater than root's in right subtree
if(seq[j] < root)
return false;
}
return isPostorderTraversal(seq, begin, i - 1) && isPostorderTraversal(seq, i, end - 1);
}
}
Problem 2:
输入一颗二叉树的根节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
1. Traceback
import java.util.ArrayList;
import java.util.Stack;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
private ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
private Stack<Integer> path = new Stack<Integer>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root == null)
return ans;
dfs(root, target, 0);
return ans;
}
private void dfs(TreeNode root, int target, int cur_sum){
if(cur_sum == target && root == null){
ans.add(new ArrayList(path));
return;
}
if(root == null)
return;
path.push(root.val);
dfs(root.left, target, cur_sum + root.val);
path.pop();
if (root.left == null && root.right == null) { //important!!! avoid repetition
return;
}
path.push(root.val);
dfs(root.right, target, cur_sum + root.val);
path.pop();
}
}
2. non-recursion
Problem 3:
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
RandomListNode currentnode = pHead;
if(pHead == null)
return null;
//clone every node
while(currentnode != null){
RandomListNode clone = new RandomListNode(currentnode.label);
RandomListNode nextnode = currentnode.next;
currentnode.next = clone;
clone.next = nextnode;
currentnode = nextnode;
}
//clone oldnode.random to newnode.random
currentnode = pHead;
while(currentnode != null){
currentnode.next.random = currentnode.random == null ? null : currentnode.random.next;
currentnode = currentnode.next.next;
}
//separate new list from old list
currentnode = pHead;
RandomListNode newhead = pHead.next;
while(currentnode != null){
RandomListNode clone = currentnode.next;
currentnode.next = clone.next;
clone.next = clone.next == null ? null : clone.next.next;
currentnode = currentnode.next;
}
return newhead;
}
}
Problem 4:
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
1. inorder traversal
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.ArrayList;
public class Solution {
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null)
return null;
ArrayList<TreeNode> list = new ArrayList<>();
Convert(pRootOfTree, list);
return Convert(list);
}
//inorder traversal
public void Convert(TreeNode pRootOfTree, ArrayList<TreeNode> list){
if(pRootOfTree.left != null)
Convert(pRootOfTree.left, list);
list.add(pRootOfTree);
if(pRootOfTree.right != null)
Convert(pRootOfTree.right, list);
return;
}
// adjust every node's pointers
public TreeNode Convert(ArrayList<TreeNode> list){
for(int i = 0; i < list.size() - 1; i++){
list.get(i).right = list.get(i + 1);
list.get(i + 1).left = list.get(i);
}
return list.get(0);
}
}
2. traverse from right subtree and adjust every node's pointer in the meanwhile
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.ArrayList;
public class Solution {
TreeNode pre = null;
public TreeNode Convert(TreeNode pRootOfTree){
if(pRootOfTree == null )
return null;
Convert(pRootOfTree.right);
if(pre == null){
pre = pRootOfTree;
}
else{
pRootOfTree.right = pre;
pre.left = pRootOfTree;
pre = pRootOfTree;
}
Convert(pRootOfTree.left);
return pre;
}
}