知识点——树
jz_01_19_shu
二叉树的下一个结点
题目描述
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
/*
public class TreeLinkNode {
int val;
TreeLinkNode left = null;
TreeLinkNode right = null;
TreeLinkNode next = null;
TreeLinkNode(int val) {
this.val = val;
}
}
*/
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.next.left==pNode){
return pNode.next;
}
pNode=pNode.next;
}
return null;
}
}
解析:
思路:首先知道中序遍历的规则是:左根右,然后作图
结合图,我们可发现分成两大类:1、有右子树的,那么下个结点就是右子树最左边的点;(eg:D,B,E,A,C,G) 2、没有右子树的,也可以分成两类,a)是父节点左孩子(eg:N,I,L) ,那么父节点就是下一个节点 ; b)是父节点的右孩子(eg:H,J,K,M)找他的父节点的父节点的父节点…直到当前结点是其父节点的左孩子位置。如果没有eg:M,那么他就是尾节点。
jz_01_20_shu
对称的二叉树
题目描述
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if (pRoot==null)return true;
return comRoot(pRoot.left,pRoot.right);
}
private boolean comRoot(TreeNode left,TreeNode right){
if (left==null)return right==null;
if (right==null)return false;
if (left.val!=right.val)return false;
return comRoot(left.left,right.right)&&comRoot(left.right,right.left);
}
}
解析:
首先根节点以及其左右子树,左子树的左子树和右子树的右子树相同
左子树的右子树和右子树的左子树相同即可,采用递归
非递归也可,采用栈或队列存取各级子树根节点
jz_02_01_shu
把二叉树打印成多行
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
import java.util.ArrayList;
import java.util.*;
/*
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> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> tmp = new ArrayList<Integer>();
LinkedList<TreeNode> q = new LinkedList<TreeNode>();
if(pRoot == null)
return ret;
q.add(pRoot);
int now = 1, next = 0;
while(!q.isEmpty()) {
TreeNode t = q.remove();
now--;
tmp.add(t.val);
if(t.left != null) {
q.add(t.left);
next++;
}
if(t.right != null) {
q.add(t.right);
next++;
}
if(now == 0) {
ret.add(new ArrayList<Integer>(tmp));
tmp.clear();
now = next;
next = 0;
}
}
return ret;
}
}
知识点——链表
jz_01_02_LinkedList
链表中环的入口
题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead==null || pHead.next==null)return null;
ListNode slow = pHead;
ListNode fast = pHead;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
slow = pHead;
while(slow!=fast){
slow = slow.next;
fast = fast.next;
}
if(slow == fast){
return slow;
}
}
}
return null;
}
}
jz_01_03_LinkedList
删除有序链表的重复结点(未完全弄懂,待补充笔试)
题目描述
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead)
{
if (pHead == null) return null;
ListNode p = pHead;
ListNode n = new ListNode(0);
ListNode pre = n;
n.next = pHead;
boolean flag = false;
while (p != null) {
ListNode q = p.next;
if (q == null) break;
if (q.val == p.val) {
while (q != null && q.val == p.val) {
q = q.next;
}
pre.next = q;
p = q;
} else {
if (!flag) {
n.next = p;
flag = true;
}
pre = p;
p = q;
}
}
return n.next;
}
}
jz_01_04_LinkedList
从头到尾打印链表
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
方法一:先反转链表,再依次存入数组。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ListNode preNode = null;
ListNode currNode = listNode;
while(currNode!=null){
ListNode next = currNode.next;
currNode.next = preNode;
preNode = currNode;
currNode = next;
}
ArrayList<Integer> arr = new ArrayList<Integer>();
while(preNode!=null){
arr.add(preNode.val);
preNode = preNode.next;
}
return arr;
}
}
方法二:利用栈。
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<>();
while (listNode != null){
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> arr = new ArrayList<Integer>();
while (!stack.isEmpty()){
arr.add(stack.pop());
}
return arr;
}
}