day01(树、链表)
- 二叉树的镜像
import java.util.*;
public class Solution {
public void Mirror(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
if(root==null){
return;
}
stack.push(root);
while(!stack.empty()){
TreeNode node =stack.pop();
if(node.left!=null||node.right!=null){
TreeNode temp;
temp=node.left;
node.left=node.right;
node.right=temp;
}
if(node.left != null)stack.push(node.left);
if(node.right != null)stack.push(node.right);
}
}
}
- 2.给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead==null||pHead.next==null){return null;}
ListNode p1=pHead;
ListNode p2=pHead;
while(p1.next!=null&&p2.next!=null){
p1=p1.next;
p2=p2.next.next;
if(p1==p2){
p1=pHead;
while(p1!=p2){
p1=p1.next;
p2=p2.next;}
if(p1==p2){
return p1;}}
}
return null;
}
}
- 3.在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if(pHead==null||pHead.next==null){
return pHead;
}
ListNode current=pHead.next;
if(pHead.val==current.val){
while(current!=null&&pHead.val==current.val){
conrrent=conrrent.next;
}
return deleteDuplication(ListNode conccrent);
}
else{
pHead.next= deleteDuplication(pHead.next);
return pHead;
}
}
}
- 4.输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<ListNode> stack=new Stack<>();
ArrayList<Integer>list=new ArrayList<>();
while(listNode!=null){
stack.push(listNode);
listNode=listNode.next;
}
while(!stack.empty()){
list.add(stack.pop().val);
}
return list;
}
}
- 5.输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
}
private int getDepth(TreeNode root){
if(root==null){
return 0;}
int left= getDepth(root.left);
int right=getDepth(root.right);
if(left==-1){
return -1;
}
if(right==-1){
return -1;
}
return Math.abs(left-right)>1?-1:(1+Math.max(left,right));
}
}
- 6.输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(TreeDepth(root.left),TreeDepth(root.right));
}
}
- 7.给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
public class Solution {
public TreeLinkNode GetNext(TreeLinkNode pNode)
{
if(pNode==null){
return null;
}
if(pNode.right!=null){
pNode=pNode.right;
while(pNode.left!=null){
pNode=pNode.left;
}
return pNode;
}
while(pNode.next!=null){
if(pNode==pNode.next.left)
return pNode.next;
}
pNode=pNode.next;
}
return null;
}
}
- 8、请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
import java.util.Stack;
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
Stack<TreeNode> s=new Stack<>();
if(pRoot==null){
return true;
}
s.push(pRoot.left);
s.push(pRoot.right);
while(s.isEmpty()){
TreeNode right=s.pop();
TreeNode left=s.pop();
if(left==null&&right==null)continue;
if(left==null||right==null)return false;
if(left.val!=right.val)return false;
s.push(left.left);
s.push(right.right);
s.push(left.right);
s.push(right.left);
}
return true;
}
- 9、从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
public class Solution {
ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result=new ArrayList<>();
if(pRoot==null){
return result;
}
Queue<TreeNode> layer =new LinkedList<TreeNode>();
layer.add(pRoot);
ArrayList<Integer> layerlist= new ArrayList<Integer>();
int i=0;int count=1;
while(!layer.isEmpty()){
TreeNode cur=layer.remove();
layerlist.add(cur.val);
System.out.print(cur.val+" ");
i++;
if(cur.left!=null){
layer.add(cur.left);
}
if(cur.right!=null){
layer.add(cur.right);
}
if(i==count){
System.out.println();
count=layer.size();
i=0;
result.add(layerlist);
layerlist=new ArrayList<Integer>();
}
}
return result;
}
}
- 10.之字形打印二叉树
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
Stack<TreeNode> s1=new Stack<TreeNode>();//存储奇数层节点;
s1.push(pRoot);
Stack<TreeNode> s2=new Stack<TreeNode>();//存储偶数层节点;
int layer=1;
ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
while(!s1.empty()||!s2.empty()){
if(layer%2==1){
ArrayList<Integer> list1=new ArrayList<>();
while(!s1.empty()){
TreeNode node=s1.pop();
if(node != null) {
list1.add(node.val);
System.out.print(node.val+" ");
s2.push(node.left);
s2.push(node.right);
}
}
if(!list1.isEmpty()){
list.add(list1);
layer++;
System.out.println();
}
}
else{
ArrayList<Integer> list2=new ArrayList<>();
while(!s2.empty()){
TreeNode node=s2.pop();
if(node != null) {
list2.add(node.val);
System.out.print(node.val+" ");
s1.push(node.right);
s1.push(node.left);
}
}
if(!list2.isEmpty()){
list.add(list2);
layer++;
}
}
}
return list;
}
}
- 11、给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
import java.util.Stack;
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k)
{ int i=0;
if(pRoot==null||k<=0){return null;}
Stack<TreeNode> stack =new Stack<>();
while(!stack.empty()||pRoot!=null){
while(pRoot!=null){
stack.push(pRoot);
pRoot=pRoot.left;
}
TreeNode temp=stack.pop();
i++;
if(i==k){
return temp;
}
pRoot=temp.right;
}
return null;
}
}
12、如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。我们使用Insert()方法读取数据流,使用GetMedian()方法获取当前读取数据的中位数。
public class Solution {
public void Insert(Integer num) {
}
public Double GetMedian() {
}
}