day01(树、链表)
import java.util.*;
public class Solution {
public void Mirror(TreeNode root) {
Stack stack = new Stack();
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);
}
}
}
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;
}
}
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;
}
}
}
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
public ArrayList printListFromTailToHead(ListNode listNode) {
Stack stack=new Stack<>();
ArrayListlist=new ArrayList<>();
while(listNode!=null){
stack.push(listNode);
listNode=listNode.next;
}
while(!stack.empty()){
list.add(stack.pop().val);
}
return list;
}
}
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));
}
}
public class Solution {
public int TreeDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(TreeDepth(root.left),TreeDepth(root.right));
}
}
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;
}
}
import java.util.Stack;
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
Stack 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;
}
public class Solution {
ArrayList > Print(TreeNode pRoot) {
ArrayList> result=new ArrayList<>();
if(pRoot==null){
return result;
}
Queue layer =new LinkedList();
layer.add(pRoot);
ArrayList layerlist= new ArrayList();
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();
}
}
return result;
}
}
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
public ArrayList > Print(TreeNode pRoot) {
Stack s1=new Stack();//存储奇数层节点;
s1.push(pRoot);
Stack s2=new Stack();//存储偶数层节点;
int layer=1;
ArrayList> list=new ArrayList>();
while(!s1.empty()||!s2.empty()){
if(layer%2==1){
ArrayList 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 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;
}
}
import java.util.Stack;
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k)
{ int i=0;
if(pRoot==null||k<=0){return null;}
Stack 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() {
}
}