Java面试--二叉树

面试题:讲一下二叉树的遍历

一、二叉树遍历方式
1、前序遍历:按照“根左右”,先遍历根节点,再遍历左子树 ,再遍历右子树
这里写图片描述
2、中序遍历:按照“左根右“,先遍历左子树,再遍历根节点,最后遍历右子树
这里写图片描述
3、后续遍历:按照“左右根”,先遍历左子树,再遍历右子树,最后遍历根节点
这里写图片描述
二、二叉树的java实现
定义节点类:

public class Node {
    public long data;
    public String sData;
    public Node leftChild;
    public Node rightChild;

    public Node(long value,String sData){
        this.data = value;
        this.sData = sData;
    }
}

定义树:

public class Tree {
    public Node root;
    public void insert(long value,String sData){
        Node newNode = new Node(value,sData);   //封装节点
        Node current = root;  //引用当前节点
        Node parent;          //引用父节点
        //如果root为null , 也就是第一次插入的时候
        if( root == null ){
            root = newNode;
            return;
        }
        else{
            while(true) {
                parent = current;
                if (current.data > value) {
                    current = current.leftChild;
                    if( current == null ) {
                        parent.leftChild = newNode;
                        return;
                    }
                } else {
                    current = current.rightChild;
                    if( current == null ){
                        parent.rightChild = newNode;
                        return;
                    }
                }
            }
        }
    }
    //查找节点
    public Node find(long value){
        Node current = root;
        while(current.data != value){
            if( current.data > value ){
                current = current.leftChild;
            }
            else{
                current = current.rightChild;
            }
            if(current == null){
                return null;
            }
        }
        return current;
    }
    //删除节点
}

三、二叉树遍历的java实现
1、前序遍历:

public static void preOrder(BinNode node) {
    public void preOrder(Node localLoad) {
        if( localLoad != null ){
            System.out.println(localLoad.data + "," + localLoad.sData);
            preOrder(localLoad.leftChild);
            preOrder(localLoad.rightChild);
        }
    }
}

测试:

public class TestTree {
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.insert(10,"James");
        tree.insert(20,"Tom");
        tree.insert(15,"Marry");
        tree.insert(3,"Kobe");
        tree.insert(4,"Naruto");
        tree.insert(90,"LiSi");
        tree.preOrder(tree.root);
    }
}
//插入之后的状态:
      10
 3          20
   4     15     90
//输出结果:
10,James
3,Kobe
4,Naruto
20,Tom
15,Marry
90,LiSi

2、中序遍历:

public void inOrder(Node localNode) {
    if( localNode != null ) {
        inOrder(localNode.leftChild);
        System.out.println(localNode.data + "," + localNode.sData);
        inOrder(localNode.rightChild);
    }
}

测试:

public class TestTree {
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.insert(10,"James");
        tree.insert(20,"Tom");
        tree.insert(15,"Marry");
        tree.insert(3,"Kobe");
        tree.insert(4,"Naruto");
        tree.insert(90,"LiSi");
        tree.inOrder(tree.root);
    }
}
//插入之后的状态:
      10
 3          20
   4     15     90
//输出结果:
3,Kobe
4,Naruto
10,James
15,Marry
20,Tom
90,LiSi

3、后序遍历:

public void postOrder(Node localNode) {
   if( localNode != null ) {
        postOrder(localNode.leftChild);
        postOrder(localNode.rightChild);
        System.out.println(localNode.data + "," + localNode.sData);
    }
}

测试:

public class TestTree {
    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.insert(10,"James");
        tree.insert(20,"Tom");
        tree.insert(15,"Marry");
        tree.insert(3,"Kobe");
        tree.insert(4,"Naruto");
        tree.insert(90,"LiSi");
        postOrder(tree.root);
    }
}
//插入之后的状态:
      10
 3          20
   4     15     90
//输出结果:
4,Naruto
3,Kobe
15,Marry
90,LiSi
20,Tom
10,James

Java面试的完整博客目录如下:Java笔试面试目录

转载请标明出处,原文地址:https://blog.youkuaiyun.com/weixin_41835916 如果觉得本文对您有帮助,请点击支持一下,您的支持是我写作最大的动力,谢谢。
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极星小王子

你的鼓励是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值