求中序遍历后继节点
class NextNode{
int value;
NextNode left;
NextNode right;
NextNode parent;
public NextNode(int value) {
this.value = value;
}
}
public class FindNextNode {
public static void findAllNextNode(NextNode node, List<NextNode> list){
if(node == null){
return;
}
if(node.left != null){
findAllNextNode(node.left,list);
}
list.add(node);
if(node.right != null){
findAllNextNode(node.right,list);
}
}
public static NextNode findNextNodeTest2(NextNode node){
NextNode par = node;
while(par.parent != null){
par = par.parent;
}
List<NextNode> list = new ArrayList<>();
findAllNextNode(par,list);
System.out.println(list);
for (int i = 0; i < list.size() - 1; i++) {
if(list.get(i).equals(node)){
return list.get(i + 1);
}
}
return null;
}
}
public class FindNextNode {
public static NextNode findLeftNode(NextNode node){
if(node == null){
return null;
}
if(node.left == null){
return node;
}
return findLeftNode(node.left);
}
public static NextNode findNextNodeTest(NextNode node){
if(node.right != null){
return findLeftNode(node.right);
}
NextNode parentNode = node.parent;
while(parentNode != null && parentNode.right ==node){
node = parentNode;
parentNode = node.parent;
}
return parentNode;
}
}
判断平衡二叉树
public class ACLTest {
static class MSG{
int height;
boolean isAVLTree;
public MSG() {
}
public MSG(int height, boolean isAVLTree) {
this.height = height;
this.isAVLTree = isAVLTree;
}
get()...
set()...
}
public static MSG isAVL(Node head){
if(head == null){
return new MSG(0, true);
}
MSG msgLeft = isAVL(head.left);
MSG msgRight = isAVL(head.right);
MSG msg = new MSG();
msg.height = Math.max(msgLeft.height,msgRight.height) + 1;
msg.isAVLTree = msgLeft.isAVLTree && msgRight.isAVLTree && (Math.abs(msgLeft.height - msgRight.height) <= 1);
return msg;
}
public static boolean isAVLTest(Node head){
MSG avl = isAVL(head);
return avl.isAVLTree;
}
}
派对的最大快乐值
class Employee{
public int happy;
List<Employee> subordinates;
public Employee(int happy) {
this.happy = happy;
}
}
public class MaxHappy{
static class Info{
public int withHead;
public int withOutHead;
}
public static Info process(Employee e){
if(e.subordinates == null){
return new Info(e.happy,0);
}
int withHeadHappy = e.happy;
int withOutHeadHappy = 0;
for(Employee next : e.subordinates){
Info nextInfo = process(next);
withHeadHappy += nextInfo.withOutHead;
withOutHeadHappy += Math.max(nextInfo.withHead,nextInfo.withOutHead);
}
return new Info(withHeadHappy,withOutHeadHappy);
}
public static int MaxHappyTest(Employee employee){
Info process = process(employee);
return process.withOutHead > process.withHead ? process.withOutHead : process.withHead;
}
}
返回整颗二叉树的最大距离
public class MaxDistance {
static class Info{
public int MaxDistance;
public int height;
public Info(int maxDistance, int hight) {
MaxDistance = maxDistance;
this.height = hight;
}
}
public static Info process(Node head){
if(head == null){
return new Info(0,0);
}
Info leftInfo = process(head.left);
Info rightInfo = process(head.right);
int height = Math.max(leftInfo.height,rightInfo.height) + 1;
int maxDistance = Math.max(Math.max(leftInfo.MaxDistance,rightInfo.MaxDistance)
,rightInfo.height + leftInfo.height + 1);
return new Info(maxDistance,height);
}
public static int MaxDistanceTest(Node head){
return process(head).MaxDistance;
}
}
求两个节点最近的父节点
public static void findAll(Node head, Map<Node,Node> map){
if(head == null){
return;
}
if(head.left != null){
map.put(head.left,head);
findAll(head.left,map);
}
if(head.right != null){
map.put(head.right,head);
findAll(head.right,map);
}
}
public static Node RecentlyFatherTest(Node head,Node node1,Node node2){
HashMap<Node, Node> map = new HashMap<>();
map.put(head,null);
findAll(head,map);
HashSet<Node> set = new HashSet<>();
set.add(node1);
while(true){
if (map.get(node1) == null){
break;
}
set.add(map.get(node1));
node1 = map.get(node1);
}
while(true){
if( set.contains(node2) ){
break;
}
node2 = map.get(node2);
}
return node2;
}
static class Info{
public Node ans;
public boolean findNode1;
public boolean findNode2;
public Info(Node ans, boolean findNode1, boolean findNode2) {
this.ans = ans;
this.findNode1 = findNode1;
this.findNode2 = findNode2;
}
}
public static Info RF(Node head,Node node1,Node node2){
if(head == null){
return new Info(null,false,false);
}
Info leftInfo = RF(head.left,node1,node2);
Info rightInfo = RF(head.right,node1,node2);
boolean findNode1 = head == node1 || leftInfo.findNode1 || rightInfo.findNode1;
boolean findNode2 = head == node2 || leftInfo.findNode2 || rightInfo.findNode2;
Node ans = null;
if(leftInfo.ans != null){
ans = leftInfo.ans;
}
if (rightInfo.ans != null){
ans = rightInfo.ans;
}
if(ans == null){
if(findNode1 && findNode2){
ans = head;
}
}
return new Info(ans,findNode1,findNode2);
}
public static Node RecentlyFatherTest2(Node head,Node node1,Node node2){
Info info = RF(head, node1, node2);
System.out.println(info.ans);
return info.ans;
}