題目:不分行從上到下打印二叉樹停巷。從上到下打印出二叉樹的每個節(jié)點耍攘,同一層的節(jié)點按照從左到右的順序打印。
思路:就是一個廣度優(yōu)先遍歷畔勤,先把同一層的節(jié)點存儲到隊列中蕾各,然后按照順序取出并且打印,接著把對應(yīng)的左右子節(jié)點加入到隊尾式曲,依次進(jìn)行。
解決方案:
public class Question32 {
static class BinaryTreeNode{
double value;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int value){
this.value = value;
}
}
public static void printFromTopToBottom(BinaryTreeNode treeRoot){
if (treeRoot == null) return;
Queue<BinaryTreeNode> queue = new LinkedList<>();
queue.add(treeRoot);
while (!queue.isEmpty()){
BinaryTreeNode tmpNode = queue.poll();
System.out.println(tmpNode.value);
if (tmpNode.left != null){
queue.add(tmpNode.left);
}
if (tmpNode.right != null){
queue.add(tmpNode.right);
}
}
}
public static void main(String[] args) {
BinaryTreeNode pHead = new BinaryTreeNode(1);
BinaryTreeNode pAhead = new BinaryTreeNode(3);
BinaryTreeNode pBhead = new BinaryTreeNode(5);
BinaryTreeNode pChead = new BinaryTreeNode(7);
pHead.left = pAhead;
pHead.right = pBhead;
pBhead.left = pChead;
printFromTopToBottom(pHead);
}
}
題目:分行從上到下打印二叉樹缸榛。從上到下按層打印二叉樹吝羞,同一層的節(jié)點按從左到右的順序打印,每一層打印到一行仔掸。
思路:在如上的基礎(chǔ)上脆贵,加兩個參數(shù)nextLevel和toBePrinted。nextLevel表示下一層的節(jié)點數(shù)起暮,toBePrinted表示在當(dāng)前層中還沒有打印的節(jié)點數(shù)卖氨。
解決方案:
public class Question32 {
static class BinaryTreeNode{
double value;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int value){
this.value = value;
}
}
public static void print(BinaryTreeNode treeRoot){
if (treeRoot == null) return;
Queue<BinaryTreeNode> queue = new LinkedList<>();
queue.add(treeRoot);
int nextLevel = 0;
int toBePrinted = 1;
while (!queue.isEmpty()){
BinaryTreeNode tmpNode = queue.poll();
System.out.print(tmpNode.value + " ");
if (tmpNode.left != null){
queue.add(tmpNode.left);
++nextLevel;
}
if (tmpNode.right != null){
queue.add(tmpNode.right);
++nextLevel;
}
--toBePrinted;
if (toBePrinted == 0){
System.out.println("\t");
toBePrinted = nextLevel;
nextLevel = 0;
}
}
}
public static void main(String[] args) {
BinaryTreeNode pHead = new BinaryTreeNode(1);
BinaryTreeNode pAhead = new BinaryTreeNode(3);
BinaryTreeNode pBhead = new BinaryTreeNode(5);
BinaryTreeNode pChead = new BinaryTreeNode(7);
pHead.left = pAhead;
pHead.right = pBhead;
pBhead.left = pChead;
print(pHead);
}
}
題目:之字形打印二叉樹会烙。實現(xiàn)一個函數(shù)按照之字形順序打印二叉樹,即第一層按照從左到右的順序打印筒捺,第二層按照從右到左的順序打印柏腻,其他行以此類推。
思路:構(gòu)建兩個棧系吭,奇數(shù)層五嫂,先保存左子節(jié)點再保存右子節(jié)點;偶數(shù)層肯尺,則先保存右子節(jié)點再保存左子節(jié)點
解決方案:
public class Question32 {
static class BinaryTreeNode{
double value;
BinaryTreeNode left;
BinaryTreeNode right;
public BinaryTreeNode(int value){
this.value = value;
}
}
public static void print3(BinaryTreeNode treeRoot) {
if (treeRoot == null) return;
Stack<BinaryTreeNode> s1 = new Stack<>();
Stack<BinaryTreeNode> s2 = new Stack<>();
s1.add(treeRoot);
while (!s1.isEmpty() || !s2.isEmpty()) {
if (!s1.isEmpty()){
while (!s1.isEmpty()){
BinaryTreeNode node = s1.pop();
System.out.print(node.value + " ");
if (node.left != null){
s2.add(node.left);
}
if (node.right != null){
s2.add(node.right);
}
}
System.out.println();
}else {
while (!s2.isEmpty()){
BinaryTreeNode node = s2.pop();
System.out.print(node.value + " ");
if (node.right != null){
s1.add(node.right);
}
if (node.left != null){
s1.add(node.left);
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
BinaryTreeNode pHead = new BinaryTreeNode(1);
BinaryTreeNode pAhead = new BinaryTreeNode(3);
BinaryTreeNode pBhead = new BinaryTreeNode(5);
BinaryTreeNode pChead = new BinaryTreeNode(7);
pHead.left = pAhead;
pHead.right = pBhead;
pBhead.left = pChead;
print3(pHead);
}
}