parent
fca8239636
commit
c9008b25e8
@ -0,0 +1,129 @@
|
|||||||
|
package zuolaos.jichuban;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Code16_多叉数 {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class 多叉树序列化和反序列化 {
|
||||||
|
private class Node {
|
||||||
|
public int val;
|
||||||
|
public List<Node> children;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val, List<Node> _children) {
|
||||||
|
val = _val;
|
||||||
|
children = _children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提交时不要提交这个类
|
||||||
|
private class TreeNode {
|
||||||
|
int val;
|
||||||
|
TreeNode left;
|
||||||
|
TreeNode right;
|
||||||
|
|
||||||
|
TreeNode(int x) {
|
||||||
|
val = x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//序列化
|
||||||
|
public TreeNode encode(Node node) {
|
||||||
|
if (node == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode treeNode = new TreeNode(node.val);
|
||||||
|
treeNode.left = en(node.children);
|
||||||
|
return treeNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TreeNode en(List<Node> children) {
|
||||||
|
TreeNode head = null;
|
||||||
|
//上一个节点的位置
|
||||||
|
TreeNode last = null;
|
||||||
|
for (Node child : children) {
|
||||||
|
TreeNode treeNode = new TreeNode(child.val);
|
||||||
|
if (head == null) {
|
||||||
|
head = treeNode;
|
||||||
|
} else {
|
||||||
|
last.right = treeNode;
|
||||||
|
}
|
||||||
|
last = treeNode;
|
||||||
|
last.left = en(child.children);
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
//反序列化
|
||||||
|
public Node decode(TreeNode treeNode) {
|
||||||
|
if (treeNode == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Node node = new Node(treeNode.val, de(treeNode));
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Node> de(TreeNode root) {
|
||||||
|
List<Node> nodes = null;
|
||||||
|
while (root != null) {
|
||||||
|
if (nodes == null) {
|
||||||
|
nodes = new ArrayList<>();
|
||||||
|
}
|
||||||
|
Node cur = new Node(root.val, de(root.left));
|
||||||
|
nodes.add(cur);
|
||||||
|
root = root.right;
|
||||||
|
}
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class 不能相邻的节点快乐值更多 {
|
||||||
|
private class Info {
|
||||||
|
int yes; //当前节点去的快乐值
|
||||||
|
int no; //当前节点去的快乐值
|
||||||
|
|
||||||
|
public Info(int yes, int no) {
|
||||||
|
this.yes = yes;
|
||||||
|
this.no = no;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Employee {
|
||||||
|
public int happy;
|
||||||
|
public List<Employee> nexts;
|
||||||
|
|
||||||
|
public Employee(int h) {
|
||||||
|
happy = h;
|
||||||
|
nexts = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Info process(Employee employee) {
|
||||||
|
if (employee == null) {
|
||||||
|
return new Info(0, 0);
|
||||||
|
}
|
||||||
|
int yes = 0; //当前节点去的快乐值
|
||||||
|
int no = 0; //当前节点去的快乐值
|
||||||
|
for (Employee next : employee.nexts) {
|
||||||
|
Info nextInfo = process(next);
|
||||||
|
yes += nextInfo.no;
|
||||||
|
no += Math.max(nextInfo.yes, nextInfo.no);
|
||||||
|
}
|
||||||
|
return new Info(yes, no);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue