第16题:层序遍历一棵二叉树


欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/45151907


第16题:输入一棵二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。

这里写图片描述

这里也是利用图的宽度优先遍历算法(BFS),所以在第15题的基础上进行简单地修改就得到了层序遍历算法


代码

package test016;

import common.BTNode;

import java.util.ArrayList;

/**
 * Created by cq on 2015/4/20.
 * 第16题:输入一棵二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。
 */
public class Test016 {
    //层序遍历,依然是BFS的思想
    public static void levelorderTraversal(BTNode bTree){
        if (bTree == null){
            return;
        }

        BTNode currentBTNode = null;
        //使用ArrayList建一个队列,保存还未被遍历到的子树
        ArrayList<BTNode> untraversedBTrees = new ArrayList<BTNode>();
        untraversedBTrees.add(bTree);

        //只要存在未被遍历的子树,就继续循环
        while (!untraversedBTrees.isEmpty()){
            //取出第一个子树
            currentBTNode = untraversedBTrees.get(0);
            untraversedBTrees.remove(0);

            BTNode curLeft = currentBTNode.getLeft();
            BTNode curRight = currentBTNode.getRight();

            //向队列中添加新发现的子树
            if (curLeft != null){
                untraversedBTrees.add(curLeft);
            }
            if (curRight != null){
                untraversedBTrees.add(curRight);
            }
            System.out.print(currentBTNode.getValue()+" ");
        }
    }
    public static void main(String[] args){
        BTNode bTree = new BTNode(8);
        BTNode node2 = new BTNode(6);
        BTNode node3 = new BTNode(10);
        BTNode node4 = new BTNode(5);
        BTNode node5 = new BTNode(7);
        BTNode node6 = new BTNode(9);
        BTNode node7 = new BTNode(11);
        bTree.setLeft(node2);
        bTree.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        node3.setRight(node7);

        levelorderTraversal(bTree);
        System.out.println();
    }
}



执行结果

Connected to the target VM, address: '127.0.0.1:7130', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:7130', transport: 'socket'
8 6 10 5 7 9 11 

Process finished with exit code 0



接下来准备找个时间把做的题上传到GitHub,如有需要,方便下载,也顺便捣鼓捣鼓GitHub ^_^。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于 C 语言的二叉树遍历和统计叶子节点数和深度的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct TreeNode { char data; struct TreeNode *left; struct TreeNode *right; } TreeNode; // 创建二叉树 TreeNode* createTree() { char c; scanf("%c", &c); if (c == '^') { return NULL; } TreeNode *root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = c; root->left = createTree(); root->right = createTree(); return root; } // 先序遍历 void preOrder(TreeNode *root) { if (root == NULL) { return; } printf("%c ", root->data); preOrder(root->left); preOrder(root->right); } // 中序遍历 void inOrder(TreeNode *root) { if (root == NULL) { return; } inOrder(root->left); printf("%c ", root->data); inOrder(root->right); } // 后序遍历 void postOrder(TreeNode *root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%c ", root->data); } // 层序遍历 void levelOrder(TreeNode *root) { if (root == NULL) { return; } TreeNode *queue[1000]; int front = 0, rear = 0; queue[rear++] = root; while (front != rear) { TreeNode *temp = queue[front++]; printf("%c ", temp->data); if (temp->left != NULL) { queue[rear++] = temp->left; } if (temp->right != NULL) { queue[rear++] = temp->right; } } } // 统计叶子节点数 int countLeaf(TreeNode *root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return countLeaf(root->left) + countLeaf(root->right); } // 计算树的深度 int depth(TreeNode *root) { if (root == NULL) { return 0; } int leftDepth = depth(root->left); int rightDepth = depth(root->right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } int main() { printf("请输入二叉树,用^表示空:\n"); TreeNode *root = createTree(); printf("\n先序遍历结果="); preOrder(root); printf("\n中序遍历结果="); inOrder(root); printf("\n后序遍历结果="); postOrder(root); printf("\n层序遍历结果="); levelOrder(root); printf("\n叶子数=%d\n", countLeaf(root)); printf("树的深度=%d\n", depth(root)); return 0; } ``` 注意事项: 1. 输入时请保证二叉树的形式正确,即左右子树都用 '^' 表示为空。 2. 叶子节点数的计算方法为:左右子树都为空时为 1,否则为左子树叶子节点数加上右子树叶子节点数之和。 3. 深度的计算方法为:左右子树深度较大的值加上 1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值