2021年12月第五周学习周记
二叉树
二叉树节点结构:
class Node <V>{
V value;
Node left;
Node right;
}
当可以通过左树和右树获得信息来解决问题的大多都可以使用递归套路解
用递归和非递归分别实现二叉树的先、中、后序遍历
package class05 ;
import java. util. Stack ;
public class Code01_PreInPosTraversal {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static void preOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
System . out. print ( head. value + " " ) ;
preOrderRecur ( head. left) ;
preOrderRecur ( head. right) ;
}
public static void inOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
inOrderRecur ( head. left) ;
System . out. print ( head. value + " " ) ;
inOrderRecur ( head. right) ;
}
public static void posOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
posOrderRecur ( head. left) ;
posOrderRecur ( head. right) ;
System . out. print ( head. value + " " ) ;
}
public static void preOrderUnRecur ( Node head) {
System . out. print ( "pre-order: " ) ;
if ( head != null ) {
Stack < Node > stack = new Stack < Node > ( ) ;
stack. add ( head) ;
while ( ! stack. isEmpty ( ) ) {
head = stack. pop ( ) ;
System . out. print ( head. value + " " ) ;
if ( head. right != null ) {
stack. push ( head. right) ;
}
if ( head. left != null ) {
stack. push ( head. left) ;
}
}
}
System . out. println ( ) ;
}
public static void inOrderUnRecur ( Node head) {
System . out. print ( "in-order: " ) ;
if ( head != null ) {
Stack < Node > stack = new Stack < Node > ( ) ;
while ( ! stack. isEmpty ( ) || head != null ) {
if ( head != null ) {
stack. push ( head) ;
head = head. left;
} else {
head = stack. pop ( ) ;
System . out. print ( head. value + " " ) ;
head = head. right;
}
}
}
System . out. println ( ) ;
}
public static void posOrderUnRecur1 ( Node head) {
System . out. print ( "pos-order: " ) ;
if ( head != null ) {
Stack < Node > s1 = new Stack < Node > ( ) ;
Stack < Node > s2 = new Stack < Node > ( ) ;
s1. push ( head) ;
while ( ! s1. isEmpty ( ) ) {
head = s1. pop ( ) ;
s2. push ( head) ;
if ( head. left != null ) {
s1. push ( head. left) ;
}
if ( head. right != null ) {
s1. push ( head. right) ;
}
}
while ( ! s2. isEmpty ( ) ) {
System . out. print ( s2. pop ( ) . value + " " ) ;
}
}
System . out. println ( ) ;
}
public static void posOrderUnRecur2 ( Node h) {
System . out. print ( "pos-order: " ) ;
if ( h != null ) {
Stack < Node > stack = new Stack < Node > ( ) ;
stack. push ( h) ;
Node c = null ;
while ( ! stack. isEmpty ( ) ) {
c = stack. peek ( ) ;
if ( c. left != null && h != c. left && h != c. right) {
stack. push ( c. left) ;
} else if ( c. right != null && h != c. right) {
stack. push ( c. right) ;
} else {
System . out. print ( stack. pop ( ) . value + " " ) ;
h = c;
}
}
}
System . out. println ( ) ;
}
public static void main ( String [ ] args) {
Node head = new Node ( 5 ) ;
head. left = new Node ( 3 ) ;
head. right = new Node ( 8 ) ;
head. left. left = new Node ( 2 ) ;
head. left. right = new Node ( 4 ) ;
head. left. left. left = new Node ( 1 ) ;
head. right. left = new Node ( 7 ) ;
head. right. left. left = new Node ( 6 ) ;
head. right. right = new Node ( 10 ) ;
head. right. right. left = new Node ( 9 ) ;
head. right. right. right = new Node ( 11 ) ;
System . out. println ( "==============recursive==============" ) ;
System . out. print ( "pre-order: " ) ;
preOrderRecur ( head) ;
System . out. println ( ) ;
System . out. print ( "in-order: " ) ;
inOrderRecur ( head) ;
System . out. println ( ) ;
System . out. print ( "pos-order: " ) ;
posOrderRecur ( head) ;
System . out. println ( ) ;
System . out. println ( "============unrecursive=============" ) ;
preOrderUnRecur ( head) ;
inOrderUnRecur ( head) ;
posOrderUnRecur1 ( head) ;
posOrderUnRecur2 ( head) ;
}
}
直观打印一颗二叉树
将输出结果顺时针旋转九十度就是一颗二叉树
"v"代表右子树
“^”代表左子树
package class05 ;
public class Code02_PrintBinaryTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static void printTree ( Node head) {
System . out. println ( "Binary Tree:" ) ;
printInOrder ( head, 0 , "H" , 17 ) ;
System . out. println ( ) ;
}
public static void printInOrder ( Node head, int height, String to , int len) {
if ( head == null ) {
return ;
}
printInOrder ( head. right, height + 1 , "v" , len) ;
String val = to + head. value + to ;
int lenM = val. length ( ) ;
int lenL = ( len - lenM) / 2 ;
int lenR = len - lenM - lenL;
val = getSpace ( lenL) + val + getSpace ( lenR) ;
System . out. println ( getSpace ( height * len) + val) ;
printInOrder ( head. left, height + 1 , "^" , len) ;
}
public static String getSpace ( int num) {
String space = " " ;
StringBuffer buf = new StringBuffer ( "" ) ;
for ( int i = 0 ; i < num; i++ ) {
buf. append ( space) ;
}
return buf. toString ( ) ;
}
public static void main ( String [ ] args) {
Node head = new Node ( 1 ) ;
head. left = new Node ( - 222222222 ) ;
head. right = new Node ( 3 ) ;
head. left. left = new Node ( Integer . MIN_VALUE) ;
head. right. left = new Node ( 55555555 ) ;
head. right. right = new Node ( 66 ) ;
head. left. left. right = new Node ( 777 ) ;
printTree ( head) ;
head = new Node ( 1 ) ;
head. left = new Node ( 2 ) ;
head. right = new Node ( 3 ) ;
head. left. left = new Node ( 4 ) ;
head. right. left = new Node ( 5 ) ;
head. right. right = new Node ( 6 ) ;
head. left. left. right = new Node ( 7 ) ;
printTree ( head) ;
head = new Node ( 1 ) ;
head. left = new Node ( 1 ) ;
head. right = new Node ( 1 ) ;
head. left. left = new Node ( 1 ) ;
head. right. left = new Node ( 1 ) ;
head. right. right = new Node ( 1 ) ;
head. left. left. right = new Node ( 1 ) ;
printTree ( head) ;
}
}
二叉树的宽度优先遍历
public static void BFS ( Node head) {
if ( head == null ) {
return ;
}
Queue < Node > queue = new LinkedList < > ( ) ;
queue. add ( head) ;
while ( ! queue. isEmpty ( ) ) {
Node cur = queue. poll ( ) ;
System . out. println ( cur. value) ;
if ( cur. left != null ) {
queue. add ( cur. left) ;
}
if ( cur. right != null ) {
queue. add ( cur. right) ;
}
}
}
求二叉树的宽度
二叉树的BFS改变而来
package class05 ;
import java. util. HashMap ;
import java. util. LinkedList ;
public class Code03_TreeMaxWidth {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static int getMaxWidth ( Node head) {
if ( head == null ) {
return 0 ;
}
int maxWidth = 0 ;
int curWidth = 0 ;
int curLevel = 0 ;
HashMap < Node , Integer > levelMap = new HashMap < > ( ) ;
levelMap. put ( head, 1 ) ;
LinkedList < Node > queue = new LinkedList < > ( ) ;
queue. add ( head) ;
Node node = null ;
Node left = null ;
Node right = null ;
while ( ! queue. isEmpty ( ) ) {
node = queue. poll ( ) ;
left = node. left;
right = node. right;
if ( left != null ) {
levelMap. put ( left, levelMap. get ( node) + 1 ) ;
queue. add ( left) ;
}
if ( right != null ) {
levelMap. put ( right, levelMap. get ( node) + 1 ) ;
queue. add ( right) ;
}
if ( levelMap. get ( node) > curLevel) {
curWidth = 1 ;
curLevel = levelMap. get ( node) ;
} else {
curWidth++ ;
}
maxWidth = Math . max ( maxWidth, curWidth) ;
}
return maxWidth;
}
public static void main ( String [ ] args) {
Node head = new Node ( 5 ) ;
head. left = new Node ( 3 ) ;
head. right = new Node ( 8 ) ;
head. left. left = new Node ( 2 ) ;
head. left. right = new Node ( 4 ) ;
head. left. left. left = new Node ( 1 ) ;
head. right. left = new Node ( 7 ) ;
head. right. left. left = new Node ( 6 ) ;
head. right. right = new Node ( 10 ) ;
head. right. right. left = new Node ( 9 ) ;
head. right. right. right = new Node ( 11 ) ;
System . out. println ( getMaxWidth ( head) ) ;
}
}
判断二叉树是否为搜索二叉树
二叉查找树(二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树:
若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树。
搜索二叉树的中序遍历是升序的
方法1
package class05 ;
import java. util. LinkedList ;
public class Code04_IsBST {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static boolean isBST ( Node head) {
if ( head == null ) {
return true ;
}
LinkedList < Node > inOrderList = new LinkedList < > ( ) ;
process ( head, inOrderList) ;
long pre = Long . MIN_VALUE;
for ( Node cur : inOrderList) {
if ( pre >= cur. value) {
return false ;
}
pre = cur. value;
}
return true ;
}
public static void process ( Node node, LinkedList < Node > inOrderList) {
if ( node == null ) {
return ;
}
process ( node. left, inOrderList) ;
inOrderList. add ( node) ;
process ( node. right, inOrderList) ;
}
}
方法2
public class isBst ( ) {
public static class Node {
int value;
Node left;
Node right;
public Node ( int data) {
this . value = data;
}
}
public static class ReturnData {
public boolean isBST;
public int min;
public int max;
public ReturnData ( boolean is, int mi, int ma) {
isBST = is;
min = mi;
max = ma;
}
public static ReturnData Process ( Node x) {
if ( x == null ) {
return null ;
}
ReturnData leftData = Process ( x. left) ;
ReturnData rightData = Process ( x. right) ;
int min = x. value;
int max = x. value;
if ( leftData != null ) {
min = Math . min ( min, leftData. min) ;
max = Math . max ( max, leftData. max) ;
}
if ( rightData != null ) {
min = Math . min ( min, rightData. min) ;
max = Math . max ( max, rightData. max) ;
}
boolean isBST = true ;
if ( leftData!= null && ( ! leftData. isBST || leftData. max >= x. value) ) {
isBST = false ;
}
if ( rightData!= null && ( ! rightData. isBST || rightData. max <= x. value) ) {
isBST = false ;
}
return new ReturnData ( isBST, min, max)
}
}
}
判断二叉树是否为完全二叉树
一棵深度为k的有n个结点的二叉树,对树中的结点按从上至下、从左到右的顺序进行编号。
如果编号为i(1≤i≤n)的结点与满二叉树中编号为i的结点在二叉树中的位置相同,则这棵二叉树称为完全二叉树。
判断方法:
1) 任一节点,若只有右孩子无左孩子 --> FALSE
2) 在不违反条件1的情况下,遇到了第一个左右孩子不全的节点,之后遇到的节点皆为叶子结点
package class05 ;
import java. util. LinkedList ;
public class Code05_IsCBT {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static boolean isCBT ( Node head) {
if ( head == null ) {
return true ;
}
LinkedList < Node > queue = new LinkedList < > ( ) ;
boolean leaf = false ;
Node l = null ;
Node r = null ;
queue. add ( head) ;
while ( ! queue. isEmpty ( ) ) {
head = queue. poll ( ) ;
l = head. left;
r = head. right;
if (
( leaf && ( l != null || r != null ) )
||
( l == null && r != null ) )
{
return false ;
}
if ( l != null ) {
queue. add ( l) ;
}
if ( r != null ) {
queue. add ( r) ;
} else {
leaf = true ;
}
}
return true ;
}
}
判断二叉树是否为满二叉树
一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。
也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。
[思路]判断一棵树是否为满二叉树,即求其总节点数和树的高度
public class isFullTree{
public static boolean isF ( Node head) {
if ( ( head == null ) ) {
return true ;
}
Info data = Process ( head) ;
return data. nodes == ( 1 << data. height) - 1 ;
}
public static class Info {
public int height;
public int nodes;
public Info ( int h, int n) {
height = h;
nodes = n;
}
}
public static Info Process ( Node x) {
if ( x == null ) {
return new Info ( 0 , 0 ) ;
}
Info leftData = Process ( x. left) ;
Info rightData = Process ( x. right) ;
int height = Math . max ( leftData. height, rightData. height) + 1 ;
int nodes = leftData. nodes + rightData. nodes + 1 ;
return new Info ( height, nodes) ;
}
}
判断二叉树是否为平衡二叉树
平衡树(Balance Tree,BT) 指的是,任意节点的子树的高度差都小于等于1
package class05 ;
public class Code06_IsBalancedTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static boolean isBalanced ( Node head) {
return process ( head) . isBalanced;
}
public static class ReturnType {
public boolean isBalanced;
public int height;
public ReturnType ( boolean isB, int hei) {
isBalanced = isB;
height = hei;
}
}
public static ReturnType process ( Node x) {
if ( x == null ) {
return new ReturnType ( true , 0 ) ;
}
ReturnType leftData = process ( x. left) ;
ReturnType rightData = process ( x. right) ;
int height = Math . max ( leftData. height, rightData. height) ;
boolean isBalanced = leftData. isBalanced && rightData. isBalanced
&& Math . abs ( leftData. height - rightData. height) < 2 ;
return new ReturnType ( isBalanced, height) ;
}
}
求最低公共祖先节点
【题目】给定两个二叉树的节点Node1和Node2,找到他们的最低公共祖先节点
package class05 ;
import java. util. HashMap ;
import java. util. HashSet ;
public class Code07_LowestCommonAncestor {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static Node lowestAncestor ( Node head, Node o1, Node o2) {
if ( head == null || head == o1 || head == o2) {
return head;
}
Node left = lowestAncestor ( head. left, o1, o2) ;
Node right = lowestAncestor ( head. right, o1, o2) ;
if ( left != null && right != null ) {
return head;
}
return left != null ? left : right;
}
public static class Record1 {
private HashMap < Node , Node > map;
public Record1 ( Node head) {
map = new HashMap < Node , Node > ( ) ;
if ( head != null ) {
map. put ( head, null ) ;
}
setMap ( head) ;
}
private void setMap ( Node head) {
if ( head == null ) {
return ;
}
if ( head. left != null ) {
map. put ( head. left, head) ;
}
if ( head. right != null ) {
map. put ( head. right, head) ;
}
setMap ( head. left) ;
setMap ( head. right) ;
}
public Node query ( Node o1, Node o2) {
HashSet < Node > path = new HashSet < Node > ( ) ;
while ( map. containsKey ( o1) ) {
path. add ( o1) ;
o1 = map. get ( o1) ;
}
while ( ! path. contains ( o2) ) {
o2 = map. get ( o2) ;
}
return o2;
}
}
public static class Record2 {
private HashMap < Node , HashMap < Node , Node > > map;
public Record2 ( Node head) {
map = new HashMap < Node , HashMap < Node , Node > > ( ) ;
initMap ( head) ;
setMap ( head) ;
}
private void initMap ( Node head) {
if ( head == null ) {
return ;
}
map. put ( head, new HashMap < Node , Node > ( ) ) ;
initMap ( head. left) ;
initMap ( head. right) ;
}
private void setMap ( Node head) {
if ( head == null ) {
return ;
}
headRecord ( head. left, head) ;
headRecord ( head. right, head) ;
subRecord ( head) ;
setMap ( head. left) ;
setMap ( head. right) ;
}
private void headRecord ( Node n, Node h) {
if ( n == null ) {
return ;
}
map. get ( n) . put ( h, h) ;
headRecord ( n. left, h) ;
headRecord ( n. right, h) ;
}
private void subRecord ( Node head) {
if ( head == null ) {
return ;
}
preLeft ( head. left, head. right, head) ;
subRecord ( head. left) ;
subRecord ( head. right) ;
}
private void preLeft ( Node l, Node r, Node h) {
if ( l == null ) {
return ;
}
preRight ( l, r, h) ;
preLeft ( l. left, r, h) ;
preLeft ( l. right, r, h) ;
}
private void preRight ( Node l, Node r, Node h) {
if ( r == null ) {
return ;
}
map. get ( l) . put ( r, h) ;
preRight ( l, r. left, h) ;
preRight ( l, r. right, h) ;
}
public Node query ( Node o1, Node o2) {
if ( o1 == o2) {
return o1;
}
if ( map. containsKey ( o1) ) {
return map. get ( o1) . get ( o2) ;
}
if ( map. containsKey ( o2) ) {
return map. get ( o2) . get ( o1) ;
}
return null ;
}
}
public static void printTree ( Node head) {
System . out. println ( "Binary Tree:" ) ;
printInOrder ( head, 0 , "H" , 17 ) ;
System . out. println ( ) ;
}
public static void printInOrder ( Node head, int height, String to , int len) {
if ( head == null ) {
return ;
}
printInOrder ( head. right, height + 1 , "v" , len) ;
String val = to + head. value + to ;
int lenM = val. length ( ) ;
int lenL = ( len - lenM) / 2 ;
int lenR = len - lenM - lenL;
val = getSpace ( lenL) + val + getSpace ( lenR) ;
System . out. println ( getSpace ( height * len) + val) ;
printInOrder ( head. left, height + 1 , "^" , len) ;
}
public static String getSpace ( int num) {
String space = " " ;
StringBuffer buf = new StringBuffer ( "" ) ;
for ( int i = 0 ; i < num; i++ ) {
buf. append ( space) ;
}
return buf. toString ( ) ;
}
public static void main ( String [ ] args) {
Node head = new Node ( 1 ) ;
head. left = new Node ( 2 ) ;
head. right = new Node ( 3 ) ;
head. left. left = new Node ( 4 ) ;
head. left. right = new Node ( 5 ) ;
head. right. left = new Node ( 6 ) ;
head. right. right = new Node ( 7 ) ;
head. right. right. left = new Node ( 8 ) ;
printTree ( head) ;
System . out. println ( "===============" ) ;
Node o1 = head. left. right;
Node o2 = head. right. left;
System . out. println ( "o1 : " + o1. value) ;
System . out. println ( "o2 : " + o2. value) ;
System . out. println ( "ancestor : " + lowestAncestor ( head, o1, o2) . value) ;
System . out. println ( "===============" ) ;
}
}
在二叉树找后继节点
【题目】现有一种新的二叉树节点类型如下:
public class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int val) {
value = val;
}
}
该结构比普通二叉树节点多了一个指向父节点的parent指针。
假设有一颗Node类型的节点组成的二叉树,树中每个节点的parent指针都已正确的指向自己的
父节点,头结点的parent指向null。
【要求】只给一个二叉树中的某个节点node,请实现返回node的后继节点的函数。
【提示】在二叉树的中序遍历的序列中,node的下一个节点叫做node的后继结点。
package class05 ;
public class Code08_SuccessorNode {
public static class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node ( int data) {
this . value = data;
}
}
public static Node getSuccessorNode ( Node node) {
if ( node == null ) {
return node;
}
if ( node. right != null ) {
return getLeftMost ( node. right) ;
} else {
Node parent = node. parent;
while ( parent != null && parent. left != node) {
node = parent;
parent = node. parent;
}
return parent;
}
}
public static Node getLeftMost ( Node node) {
if ( node == null ) {
return node;
}
while ( node. left != null ) {
node = node. left;
}
return node;
}
public static void main ( String [ ] args) {
Node head = new Node ( 6 ) ;
head. parent = null ;
head. left = new Node ( 3 ) ;
head. left. parent = head;
head. left. left = new Node ( 1 ) ;
head. left. left. parent = head. left;
head. left. left. right = new Node ( 2 ) ;
head. left. left. right. parent = head. left. left;
head. left. right = new Node ( 4 ) ;
head. left. right. parent = head. left;
head. left. right. right = new Node ( 5 ) ;
head. left. right. right. parent = head. left. right;
head. right = new Node ( 9 ) ;
head. right. parent = head;
head. right. left = new Node ( 8 ) ;
head. right. left. parent = head. right;
head. right. left. left = new Node ( 7 ) ;
head. right. left. left. parent = head. right. left;
head. right. right = new Node ( 10 ) ;
head. right. right. parent = head. right;
Node test = head. left. left;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. left. left. right;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. left;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. left. right;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. left. right. right;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. right. left. left;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. right. left;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. right;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) . value) ;
test = head. right. right;
System . out. println ( test. value + " next: " + getSuccessorNode ( test) ) ;
}
}
二叉树的序列化和反序列化
【题目】将内容中的一棵树转换成字符串形式,从字符串形式变成内存中的树
package class05 ;
import java. util. LinkedList ;
import java. util. Queue ;
public class Code09_SerializeAndReconstructTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
public static String serialByPre ( Node head) {
if ( head == null ) {
return "#!" ;
}
String res = head. value + "!" ;
res += serialByPre ( head. left) ;
res += serialByPre ( head. right) ;
return res;
}
public static Node reconByPreString ( String preStr) {
String [ ] values = preStr. split ( "!" ) ;
Queue < String > queue = new LinkedList < String > ( ) ;
for ( int i = 0 ; i != values. length; i++ ) {
queue. offer ( values[ i] ) ;
}
return reconPreOrder ( queue) ;
}
public static Node reconPreOrder ( Queue < String > queue) {
String value = queue. poll ( ) ;
if ( value. equals ( "#" ) ) {
return null ;
}
Node head = new Node ( Integer . valueOf ( value) ) ;
head. left = reconPreOrder ( queue) ;
head. right = reconPreOrder ( queue) ;
return head;
}
public static String serialByLevel ( Node head) {
if ( head == null ) {
return "#!" ;
}
String res = head. value + "!" ;
Queue < Node > queue = new LinkedList < Node > ( ) ;
queue. offer ( head) ;
while ( ! queue. isEmpty ( ) ) {
head = queue. poll ( ) ;
if ( head. left != null ) {
res += head. left. value + "!" ;
queue. offer ( head. left) ;
} else {
res += "#!" ;
}
if ( head. right != null ) {
res += head. right. value + "!" ;
queue. offer ( head. right) ;
} else {
res += "#!" ;
}
}
return res;
}
public static Node reconByLevelString ( String levelStr) {
String [ ] values = levelStr. split ( "!" ) ;
int index = 0 ;
Node head = generateNodeByString ( values[ index++ ] ) ;
Queue < Node > queue = new LinkedList < Node > ( ) ;
if ( head != null ) {
queue. offer ( head) ;
}
Node node = null ;
while ( ! queue. isEmpty ( ) ) {
node = queue. poll ( ) ;
node. left = generateNodeByString ( values[ index++ ] ) ;
node. right = generateNodeByString ( values[ index++ ] ) ;
if ( node. left != null ) {
queue. offer ( node. left) ;
}
if ( node. right != null ) {
queue. offer ( node. right) ;
}
}
return head;
}
public static Node generateNodeByString ( String val) {
if ( val. equals ( "#" ) ) {
return null ;
}
return new Node ( Integer . valueOf ( val) ) ;
}
public static void printTree ( Node head) {
System . out. println ( "Binary Tree:" ) ;
printInOrder ( head, 0 , "H" , 17 ) ;
System . out. println ( ) ;
}
public static void printInOrder ( Node head, int height, String to , int len) {
if ( head == null ) {
return ;
}
printInOrder ( head. right, height + 1 , "v" , len) ;
String val = to + head. value + to ;
int lenM = val. length ( ) ;
int lenL = ( len - lenM) / 2 ;
int lenR = len - lenM - lenL;
val = getSpace ( lenL) + val + getSpace ( lenR) ;
System . out. println ( getSpace ( height * len) + val) ;
printInOrder ( head. left, height + 1 , "^" , len) ;
}
public static String getSpace ( int num) {
String space = " " ;
StringBuffer buf = new StringBuffer ( "" ) ;
for ( int i = 0 ; i < num; i++ ) {
buf. append ( space) ;
}
return buf. toString ( ) ;
}
public static void main ( String [ ] args) {
Node head = null ;
printTree ( head) ;
String pre = serialByPre ( head) ;
System . out. println ( "serialize tree by pre-order: " + pre) ;
head = reconByPreString ( pre) ;
System . out. print ( "reconstruct tree by pre-order, " ) ;
printTree ( head) ;
String level = serialByLevel ( head) ;
System . out. println ( "serialize tree by level: " + level) ;
head = reconByLevelString ( level) ;
System . out. print ( "reconstruct tree by level, " ) ;
printTree ( head) ;
System . out. println ( "====================================" ) ;
head = new Node ( 1 ) ;
printTree ( head) ;
pre = serialByPre ( head) ;
System . out. println ( "serialize tree by pre-order: " + pre) ;
head = reconByPreString ( pre) ;
System . out. print ( "reconstruct tree by pre-order, " ) ;
printTree ( head) ;
level = serialByLevel ( head) ;
System . out. println ( "serialize tree by level: " + level) ;
head = reconByLevelString ( level) ;
System . out. print ( "reconstruct tree by level, " ) ;
printTree ( head) ;
System . out. println ( "====================================" ) ;
head = new Node ( 1 ) ;
head. left = new Node ( 2 ) ;
head. right = new Node ( 3 ) ;
head. left. left = new Node ( 4 ) ;
head. right. right = new Node ( 5 ) ;
printTree ( head) ;
pre = serialByPre ( head) ;
System . out. println ( "serialize tree by pre-order: " + pre) ;
head = reconByPreString ( pre) ;
System . out. print ( "reconstruct tree by pre-order, " ) ;
printTree ( head) ;
level = serialByLevel ( head) ;
System . out. println ( "serialize tree by level: " + level) ;
head = reconByLevelString ( level) ;
System . out. print ( "reconstruct tree by level, " ) ;
printTree ( head) ;
System . out. println ( "====================================" ) ;
head = new Node ( 100 ) ;
head. left = new Node ( 21 ) ;
head. left. left = new Node ( 37 ) ;
head. right = new Node ( - 42 ) ;
head. right. left = new Node ( 0 ) ;
head. right. right = new Node ( 666 ) ;
printTree ( head) ;
pre = serialByPre ( head) ;
System . out. println ( "serialize tree by pre-order: " + pre) ;
head = reconByPreString ( pre) ;
System . out. print ( "reconstruct tree by pre-order, " ) ;
printTree ( head) ;
level = serialByLevel ( head) ;
System . out. println ( "serialize tree by level: " + level) ;
head = reconByLevelString ( level) ;
System . out. print ( "reconstruct tree by level, " ) ;
printTree ( head) ;
System . out. println ( "====================================" ) ;
}
}
折纸问题
【题目】把一段纸条竖着放在桌子上,从纸条的下边向上方对折一次,压出折痕后展开。
此时折痕是凹下去的,即折痕凸起的地方指向纸条的背面。
如果从纸条的下边向上方连续对着2次,压出折痕后展开,此时有三条折痕,从上到下依次是
下折痕、下折痕和上折痕。
给定一个输入参数N,代表纸条都从下边向上方连续对折N次
请从上到下打印所有折痕的方向
例如:N=1时,打印:down
N=2时,打印 down down up
[思路] 从上到下即为这棵二叉树的中序遍历
package class05 ;
public class Code10_PaperFolding {
public static void printAllFolds ( int N ) {
printProcess ( 1 , N , true ) ;
}
public static void printProcess ( int i, int N , boolean down) {
if ( i > N ) {
return ;
}
printProcess ( i + 1 , N , true ) ;
System . out. println ( down ? "down " : "up " ) ;
printProcess ( i + 1 , N , false ) ;
}
public static void main ( String [ ] args) {
int N = 1 ;
printAllFolds ( N ) ;
}
}