剑指offer-问题23

package offer;

import java.util.LinkedList;
import java.util.Queue;

/**
 * offer interview 23
 */
public class Test23 {
    public static class BinaryTreeNode{
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;

        public BinaryTreeNode(){}
        public BinaryTreeNode(int value){
            this.value = value;
            this.left = null;
            this.right = null;
        }
    }

    public static void printFromToBottom(BinaryTreeNode root){
        if (null != root){
            Queue<BinaryTreeNode> list = new LinkedList<>();
            list.add(root);
            BinaryTreeNode curNode;

            while (!list.isEmpty()){
                curNode = list.remove();
                System.out.print(curNode.value + " ");

                if (curNode.left != null){
                    list.add(curNode.left);
                }

                if (curNode.right != null){
                    list.add(curNode.right);
                }
            }
        }
        System.out.println();

    }

    public static void main(String[]  args){
        //       8
        //    /    \
        //   6     10
        //  / \   / \
        // 5   7 9  11
        BinaryTreeNode root = new BinaryTreeNode(8);
        root.left = new BinaryTreeNode(6);
        root.right = new BinaryTreeNode(10);
        root.left.left = new BinaryTreeNode(5);
        root.left.right = new BinaryTreeNode(7);
        root.right.left = new BinaryTreeNode(9);
        root.right.right = new BinaryTreeNode(11);
        printFromToBottom(root);


        //         1
        //        /
        //       3
        //      /
        //     5
        //    /
        //   7
        //  /
        // 9
        BinaryTreeNode root2 = new BinaryTreeNode(1);
        root2.left = new BinaryTreeNode(3);
        root2.left.left = new BinaryTreeNode(5);
        root2.left.left.left = new BinaryTreeNode(7);
        root2.left.left.left.left = new BinaryTreeNode(9);
        printFromToBottom(root2);


        // 0
        //  \
        //   2
        //    \
        //     4
        //      \
        //       6
        //        \
        //         8
        BinaryTreeNode root3 = new BinaryTreeNode(0);
        root3.right = new BinaryTreeNode(2);
        root3.right.right = new BinaryTreeNode(4);
        root3.right.right.right = new BinaryTreeNode(6);
        root3.right.right.right.right = new BinaryTreeNode(8);

        printFromToBottom(root3);
    }




}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值