都是泪,什么前序后序中序遍历,没啥难的,先看看怎么生成吧。
//下面算是递归创建
//更容易的就是按层创建了。
@Getter @Setter public class DoubleLinkNode<T> { public T val; public DoubleLinkNode<T> parent; public DoubleLinkNode<T> left; public DoubleLinkNode<T> right; public static void main(String[] args) { DoubleLinkNode root = generate(5); System.out.println(); } public static DoubleLinkNode generate(int needLevel) { DoubleLinkNode root = new DoubleLinkNode(); root.val = 1; int currentLevel = 1; int parentValue = (int) root.val; addChild(root, currentLevel, needLevel, parentValue); return root; } private static void addChild(DoubleLinkNode root, int nowLevel, int needLevel, int parentValue) { int currentLevel = nowLevel + 1; if (currentLevel > needLevel) { return; } DoubleLinkNode left = new DoubleLinkNode(); root.left = left; left.parent = root; int leftValue = 2 * parentValue; left.val = leftValue; addChild(left, currentLevel, needLevel, leftValue); DoubleLinkNode right = new DoubleLinkNode(); root.right = right; right.parent = root; int rightValue = 2 * parentValue + 1; right.val = rightValue; addChild(right, currentLevel, needLevel, rightValue); } }
乐享:知识积累,快乐无限。