ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

叶子相似的树

2019-09-11 18:00:49  阅读:297  来源: 互联网

标签:node Node rightChild nodeList get 叶子 相似 null


给定一颗叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两颗二叉树的叶值序列是相同,那么我们就认为它们是叶相似的。
如果给定的两个头结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false。
创建二叉树,遍历叶子结点,比较根结点是否相同

代码:

class Tree{

    private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    private static List<Node> nodeList = null;
    private static class Node {
        Node leftChild;
        Node rightChild;
        int data;
        Node(int newData) {
            leftChild = null;
            rightChild = null;
            data = newData;
        }
    }

    public void createBinTree() {
        nodeList = new LinkedList<Node>();
        // 将一个数组的值依次转换为Node节点
        for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
            nodeList.add(new Node(array[nodeIndex]));
        }
        // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
        for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {
            // 左孩子
            nodeList.get(parentIndex).leftChild = nodeList
                    .get(parentIndex * 2 + 1);
            // 右孩子
            nodeList.get(parentIndex).rightChild = nodeList
                    .get(parentIndex * 2 + 2);
        }
        // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
        int lastParentIndex = array.length / 2 - 1;
        // 左孩子
        nodeList.get(lastParentIndex).leftChild = nodeList
                .get(lastParentIndex * 2 + 1);
        // 右孩子,如果数组的长度为奇数才建立右孩子
        if (array.length % 2 == 1) {
            nodeList.get(lastParentIndex).rightChild = nodeList
                    .get(lastParentIndex * 2 + 2);
        }
    }
    /**
     * 先序遍历
     */
    public static void preOrderTraverse(Node node) {
        if (node == null)
            return;
        System.out.print(node.data + " ");
        preOrderTraverse(node.leftChild);
        preOrderTraverse(node.rightChild);
    }
    /**
     * 中序遍历
     */
    public static void inOrderTraverse(Node node) {
        if (node == null)
            return;
        inOrderTraverse(node.leftChild);
        System.out.print(node.data + " ");
        inOrderTraverse(node.rightChild);
    }
    /**
     * 后序遍历
     * 将叶子结点全部存放在一个List中
     */
    static List<Integer> l = new ArrayList<>();
    public static List<Integer> postOrderTraverse(Node node) {
        if (node == null)
            return null;
        postOrderTraverse(node.leftChild);
        postOrderTraverse(node.rightChild);
//        System.out.print(node.data + " ");
        if (node.leftChild==null && node.rightChild==null){
            l.add(node.data);
        }
        return l;
    }
    
    /**
     * 判断两个树的根结点知否相等
     */
    public static boolean leafSimilar(Node root1, Node root2) {
        if(root1.data == root2.data){
            List l1 = postOrderTraverse(root1);
            List l2 = postOrderTraverse(root2);
            if (l1 == l2)
                return true;
            else
                return false;
        }else {
            return false;
        }
    }
}

标签:node,Node,rightChild,nodeList,get,叶子,相似,null
来源: https://blog.csdn.net/Julyyt_/article/details/100743875

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有