题目:
• 请实现一个函数按照之字形顺序打印二叉树。
• 即第一行按照从左到右的顺序打印;
• 第二层按照从右到左的顺序打印;
• 第三行再按照从左到右的顺序打印,其他行以此类推。
如果当前打印的是奇数层(第一层、第三层等),
则先保存左子节点再保存右子节点到第一个栈里;
如果当前打印的是偶数层(第二层 、第四层等),
则先保存右子节点再保存在子节点到第二个栈里。
方法的代码如下
public void printTree(TreeNode root){
if(root==null){
return;
}
LinkedList<TreeNode> stack1=new LinkedList<>();
LinkedList<TreeNode> stack2=new LinkedList<>();
stack1.push(root);
TreeNode node=null;
while(!stack1.isEmpty()||!stack2.isEmpty()){
while(!stack1.isEmpty()){
node=stack1.pop();
System.out.print(node.val+" ");//因为是栈,所以想要反向打印出来的时候就要正向压入栈中
if(node.left!=null){
stack2.push(node.left);
}
if(node.right!=null){
stack2.push(node.right);
}
}
System.out.println();
while(!stack2.isEmpty()){
node=stack2.pop();
System.out.print(node.val+" ");//因为是栈,所以想要反向打印出来的时候就要正向压入栈中
if(node.right!=null){
stack1.push(node.right);
}
if(node.left!=null){
stack1.push(node.left);
}
}
System.out.println();
}
}
完整的代码
package com.helan.d;
import java.util.LinkedList;
public class PirntfTreeNode {
public static class TreeNode{
int val;
TreeNode right;
TreeNode left;
public TreeNode(){
}
public TreeNode(int val){
this.val=val;
}
}
public void printTree(TreeNode root){
if(root==null){
return;
}
LinkedList<TreeNode> stack1=new LinkedList<>();
LinkedList<TreeNode> stack2=new LinkedList<>();
stack1.push(root);
TreeNode node=null;
while(!stack1.isEmpty()||!stack2.isEmpty()){
while(!stack1.isEmpty()){
node=stack1.pop();
System.out.print(node.val+" ");//因为是栈,所以想要反向打印出来的时候就要正向压入栈中
if(node.left!=null){
stack2.push(node.left);
}
if(node.right!=null){
stack2.push(node.right);
}
}
System.out.println();
while(!stack2.isEmpty()){
node=stack2.pop();
System.out.print(node.val+" ");//因为是栈,所以想要反向打印出来的时候就要正向压入栈中
if(node.right!=null){
stack1.push(node.right);
}
if(node.left!=null){
stack1.push(node.left);
}
}
System.out.println();
}
}
public TreeNode createBinaryTreeNode(int value){
TreeNode pNode=new TreeNode();
pNode.val=value;
pNode.left=null;
pNode.right=null;
return pNode;
}
public void connectTreeNodes(TreeNode pParent,TreeNode pLeft,TreeNode pRight){
if(pParent!=null){
pParent.left=pLeft;
pParent.right=pRight;
}
}
public void print(TreeNode pRoot){
printTreeNode(pRoot);
}
private void printTreeNode(TreeNode pNode) {
if(pNode!=null){
System.out.printf("节点的值是:%d\n",pNode.val);
if(pNode.left!=null){
System.out.printf("节点的左子节点的值是值是:%d\n",pNode.left.val);
}else{
System.out.printf("节点的左子节点为空\n");
}
if(pNode.right!=null){
System.out.printf("节点的右子节点的值是值是:%d\n",pNode.right.val);
}else{
System.out.printf("节点的右子节点为空\n");
}
}else{
System.out.println("节点为空\n");
}
System.out.println();
}
// ====================测试代码====================
void test(String testName, TreeNode pRoot){
if(testName != null)
System.out.printf("%s begins: \n", testName);
printTree(pRoot);
System.out.printf("The nodes from top to bottom, from left to right are: \n");
print(pRoot);
System.out.printf("\n\n");
}
// 10
// / \
// 6 14
// /\ /\
// 4 8 12 16
void test1() {
TreeNode pNode10 = createBinaryTreeNode(10);
TreeNode pNode6 = createBinaryTreeNode(6);
TreeNode pNode14 = createBinaryTreeNode(14);
TreeNode pNode4 = createBinaryTreeNode(4);
TreeNode pNode8 = createBinaryTreeNode(8);
TreeNode pNode12 = createBinaryTreeNode(12);
TreeNode pNode16 = createBinaryTreeNode(16);
connectTreeNodes(pNode10, pNode6, pNode14);
connectTreeNodes(pNode6, pNode4, pNode8);
connectTreeNodes(pNode14, pNode12, pNode16);
test("Test1", pNode10);
}
// 5
// /
// 4
// /
// 3
// /
// 2
// /
// 1
void test2()
{
TreeNode pNode5 = createBinaryTreeNode(5);
TreeNode pNode4 = createBinaryTreeNode(4);
TreeNode pNode3 = createBinaryTreeNode(3);
TreeNode pNode2 = createBinaryTreeNode(2);
TreeNode pNode1 = createBinaryTreeNode(1);
connectTreeNodes(pNode5, pNode4, null);
connectTreeNodes(pNode4, pNode3, null);
connectTreeNodes(pNode3, pNode2, null);
connectTreeNodes(pNode2, pNode1, null);
test("Test2", pNode5);
}
// 1
// \
// 2
// \
// 3
// \
// 4
// \
// 5
void test3()
{
TreeNode pNode1 = createBinaryTreeNode(1);
TreeNode pNode2 = createBinaryTreeNode(2);
TreeNode pNode3 = createBinaryTreeNode(3);
TreeNode pNode4 = createBinaryTreeNode(4);
TreeNode pNode5 = createBinaryTreeNode(5);
connectTreeNodes(pNode1, null, pNode2);
connectTreeNodes(pNode2, null, pNode3);
connectTreeNodes(pNode3, null, pNode4);
connectTreeNodes(pNode4, null, pNode5);
test("Test3", pNode1);
}
// 树中只有1个结点
void test4(){
TreeNode pNode1 = createBinaryTreeNode(1);
test("Test4", pNode1);
}
// 树中没有结点
void test5(){
test("Test5", null);
}
public static void main(String[] args) {
new PirntfTreeNode().test1();
new PirntfTreeNode().test2();
new PirntfTreeNode().test3();
new PirntfTreeNode().test4();
new PirntfTreeNode().test5();
}
}