public
class code01_dgmodel
}//先序遍歷 頭左右
public
void
preorderrecur(node head)
system.out.println(head.value+ " ");
preorderrecur(head.left);
preorderrecur(head.right);
}//中序遍歷 左頭右
public
void
inorderrecur(node head)
inorderrecur(head.left);
system.out.println(head.value + " ");
inorderrecur(head.right);
}//後續遍歷 左右頭
public
void
inposrecur(node head)
inposrecur(head.left);
inposrecur(head.right);
system.out.println(head.value+ " ");
}}
在**上展示列印遞迴版本的三種順序的方式就是列印時機的不同
public
class code01_nodgmodel
}// 前序 中左右
public
void
preorderunrecur(node head)
if (head.left != null)
}system.out.println();}}
// 中序 左中右
public
static
void
inorderunrecur(node head) else }}
system.out.println();
}// 後序
public
static
void
posorderunrecur1(node head)
if (head.right != null)
}while (!s2.isempty())
}system.out.println();
}}
後繼節點:在中序遍歷中節點的後乙個節點叫該節點的後繼節點
public
class code03_successornode
}public
static node getsuccessornode(node node)
//乙個節點如果有右子樹 它的後繼節點就是該節點右子樹上最左的節點 因為整體是中序遍歷 左中右
if(node.right!=null)else
return parent;}}
private
static node getleftmost(node node)
while(node.left!=null)
return node;
}}
public
class code04_serializeandreconstructtree
}public
static string serialbypre(node head)
//按照先序方式來序列化
string res = head.value + "!";
res += serialbypre(head.left);
res += serialbypre(head.right);
return res;
}public
static node reconbyprestring(string prestr)
return reconpreorder(queue);
}public
static node reconpreorder(queuequeue)
node head = new node(integer.valueof(value));
head.left = reconpreorder(queue);
head.right = reconpreorder(queue);
return head;
}//按層序列化
public
static string serialbylevel(node head)
string res = head.value + "!";
queuequeue = new linkedlist();
queue.offer(head);
while (!queue.isempty()) else
if (head.right != null) else
}return res;
}}
public
class code05_isbalancedtree
}public
static
class returndata
}public
static boolean isb(node head)
public
static returndata process(node head)
returndata leftdata = process(head.left);
if(!leftdata.isbalance)
returndata rightdata = process(head.right);
if(!rightdata.isbalance)
if(math.abs(leftdata.h - rightdata.h)>1)
return
new returndata(true,math.max(leftdata.h,rightdata.h)+1);}}
public
static
void
main(string args)
判斷一棵樹是否是搜尋二叉樹(若它的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值;若它的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值;它的左、右子樹也分別為二叉排序樹)
public
class
code06_isbstandcbt
private
static
int pre = integer.min_value;
public
boolean
isbst(node head)
//先判斷左子數
boolean judgeleft = isbst(head.left);
//值遞增並且judgeleft為真
if(head.data>=pre&&judgeleft==true)else
boolean judgeright = isbst(head.right);
return judgeright;
}}
二叉樹相關面試題
void treepreorderbyloop treenode root return void treeinorderbyloop treenode root 若cur為空,取棧頂元素,訪問,出棧 treenode top null int ret gettop stack,top if ret...
二叉樹相關面試題
二叉樹的建立 1.求二叉樹節點個數 private static int count 0 public static void getsize node root count getsize root.left getsize root.right public static int getsize...
二叉樹相關的面試題《一》
1.判斷b是否為a的子結構 思路 b如果為a的子結構,要麼b和a一樣,要麼b就是a的左子樹或右子樹的一部分 對a的當前節點來說,如果和b的節點相同,那麼a的左右子樹的節點都要和b相同,的關係 對a的當前節點來說,如果和b的節點不相同,那麼在判斷左右子樹上是否有相同的,的關係 圖為 為 bool is...