当前位置: 首页 > news >正文

P117、面试题18:树的子结构

题目:输入两棵二叉树A和B,判断B是不是A的子结构。二叉树结点的定义如下:
struct BinaryTreeNode{
       int  m_nValue;
       BinaryTreeNode*    m_pLeft;
       BinaryTreeNode*    m_pRight;
}

代码实现:

package com.yyq;

/**
 * Created by Administrator on 2015/9/15.
 */
public class SubstructureInTree {
    public static boolean hashSubTree(BinaryTreeNode pRoot1, BinaryTreeNode pRoot2) {
        boolean result = false;
        if (pRoot1 != null && pRoot2 != null) {
            if (pRoot1.getM_nValue() == pRoot2.getM_nValue()) {
                result = doesTree1HaveTree2(pRoot1, pRoot2);
            }
            if (!result) {
                result = hashSubTree(pRoot1.getM_pLeft(), pRoot2);
            }
            if (!result) {
                result = hashSubTree(pRoot1.getM_pRight(), pRoot2);
            }
        }
        return result;
    }

    public static boolean doesTree1HaveTree2(BinaryTreeNode pRoot1, BinaryTreeNode pRoot2) {
        if (pRoot2 == null)
            return true;
        if (pRoot1 == null)
            return false;
        if (pRoot1.getM_nValue() != pRoot2.getM_nValue())
            return false;
        return doesTree1HaveTree2(pRoot1.getM_pLeft(), pRoot2.getM_pLeft()) && doesTree1HaveTree2(pRoot1.getM_pRight(), pRoot2.getM_pRight());
    }

    // ====================测试代码====================
    public static void Test(String testName, BinaryTreeNode pRoot1, BinaryTreeNode pRoot2, boolean expected) {
        if (hashSubTree(pRoot1, pRoot2) == expected)
            System.out.println(testName + " passed.");
        else
            System.out.println(testName + " fail.");
    }

    // 树中结点含有分叉,树B是树A的子结构
//                  8                8
//              /       \           / \
//             8         7         9   2
//           /   \
//          9     2
//               / \
//              4   7
    public static void Test1() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(7);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(2);
        BinaryTreeNode pNodeA6 = new BinaryTreeNode(4);
        BinaryTreeNode pNodeA7 = new BinaryTreeNode(7);

        pNodeA1.connectTreeNodes(pNodeA2, pNodeA3);
        pNodeA2.connectTreeNodes(pNodeA4, pNodeA5);
        pNodeA5.connectTreeNodes(pNodeA6, pNodeA7);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(pNodeB2, pNodeB3);
        Test("Test1", pNodeA1, pNodeB1, true);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树中结点含有分叉,树B不是树A的子结构
//                  8                8
//              /       \           / \
//             8         7         9   2
//           /   \
//          9     3
//               / \
//              4   7
    public static void Test2() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(7);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(3);
        BinaryTreeNode pNodeA6 = new BinaryTreeNode(4);
        BinaryTreeNode pNodeA7 = new BinaryTreeNode(7);

        pNodeA1.connectTreeNodes(pNodeA2, pNodeA3);
        pNodeA2.connectTreeNodes(pNodeA4, pNodeA5);
        pNodeA5.connectTreeNodes(pNodeA6, pNodeA7);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(pNodeB2, pNodeB3);

        Test("Test2", pNodeA1, pNodeB1, false);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树中结点只有左子结点,树B是树A的子结构
//                8                  8
//              /                   /
//             8                   9
//           /                    /
//          9                    2
//         /
//        2
//       /
//      5
    public static void Test3() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(2);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(5);

        pNodeA1.connectTreeNodes(pNodeA2, null);
        pNodeA2.connectTreeNodes(pNodeA3, null);
        pNodeA3.connectTreeNodes(pNodeA4, null);
        pNodeA4.connectTreeNodes(pNodeA5, null);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(pNodeB2, null);
        pNodeB2.connectTreeNodes(pNodeB3, null);

        Test("Test3", pNodeA1, pNodeB1, true);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树中结点只有左子结点,树B不是树A的子结构
//                8                  8
//              /                   /
//             8                   9
//           /                    /
//          9                    3
//         /
//        2
//       /
//      5
    public static void Test4() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(2);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(5);

        pNodeA1.connectTreeNodes(pNodeA2, null);
        pNodeA2.connectTreeNodes(pNodeA3, null);
        pNodeA3.connectTreeNodes(pNodeA4, null);
        pNodeA4.connectTreeNodes(pNodeA5, null);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(3);

        pNodeB1.connectTreeNodes(pNodeB2, null);
        pNodeB2.connectTreeNodes(pNodeB3, null);

        Test("Test4", pNodeA1, pNodeB1, false);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树中结点只有右子结点,树B是树A的子结构
//       8                   8
//        \                   \
//         8                   9
//          \                   \
//           9                   2
//            \
//             2
//              \
//               5
    public static void Test5() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(2);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(5);

        pNodeA1.connectTreeNodes(null, pNodeA2);
        pNodeA2.connectTreeNodes(null, pNodeA3);
        pNodeA3.connectTreeNodes(null, pNodeA4);
        pNodeA4.connectTreeNodes(null, pNodeA5);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(null, pNodeB2);
        pNodeB2.connectTreeNodes(null, pNodeB3);

        Test("Test5", pNodeA1, pNodeB1, true);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树A中结点只有右子结点,树B不是树A的子结构
//       8                   8
//        \                   \
//         8                   9
//          \                 / \
//           9               3   2
//            \
//             2
//              \
//               5
    public static void Test6() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(2);
        BinaryTreeNode pNodeA5 = new BinaryTreeNode(5);

        pNodeA1.connectTreeNodes(null, pNodeA2);
        pNodeA2.connectTreeNodes(null, pNodeA3);
        pNodeA3.connectTreeNodes(null, pNodeA4);
        pNodeA4.connectTreeNodes(null, pNodeA5);

        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(3);
        BinaryTreeNode pNodeB4 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(null, pNodeB2);
        pNodeB2.connectTreeNodes(pNodeB3, pNodeB4);

        Test("Test6", pNodeA1, pNodeB1, false);
        pNodeA1 = null;
        pNodeB1 = null;
    }

    // 树A为空树
    public static void Test7() {
        BinaryTreeNode pNodeB1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeB2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeB3 = new BinaryTreeNode(3);
        BinaryTreeNode pNodeB4 = new BinaryTreeNode(2);

        pNodeB1.connectTreeNodes(null, pNodeB2);
        pNodeB2.connectTreeNodes(pNodeB3, pNodeB4);

        Test("Test7", null, pNodeB1, false);
        pNodeB1 = null;
    }

    // 树B为空树
    public static void Test8() {
        BinaryTreeNode pNodeA1 = new BinaryTreeNode(8);
        BinaryTreeNode pNodeA2 = new BinaryTreeNode(9);
        BinaryTreeNode pNodeA3 = new BinaryTreeNode(3);
        BinaryTreeNode pNodeA4 = new BinaryTreeNode(2);

        pNodeA1.connectTreeNodes(null, pNodeA2);
        pNodeA2.connectTreeNodes(pNodeA3, pNodeA4);

        Test("Test8", pNodeA1, null, false);
        pNodeA1 = null;
    }

    // 树A和树B都为空
    public static void Test9() {
        Test("Test9", null, null, false);
    }

    public static void main(String[] args) {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
        Test6();
        Test7();
        Test8();
        Test9();
    }
}
 
输出结果:
Test1 passed.
Test2 passed.
Test3 passed.
Test4 passed.
Test5 passed.
Test6 passed.
Test7 passed.
Test8 passed.
Test9 passed.

转载于:https://www.cnblogs.com/yangyquin/p/4933284.html

相关文章:

  • [CF543A]/[CF544C]Writing Code
  • IOS 百度地图点聚合使用
  • PHP和MySQL Web开发从新手到高手,第2天-怎样用zend创建PHP项目
  • 支持 Windows 10 最新 PerMonitorV2 特性的 WPF 多屏高 DPI 应用开发
  • 如何使用 Quagga BGP(边界网关协议)路由器来过滤 BGP 路由
  • 常用正则表达式(高亮,markdown)
  • 一些资料
  • Selenium库简介
  • shell 相关操作
  • Android 内存分析
  • ASP.NET的几个试题(《C#与.NET程序员面试宝典》)
  • 可以使你成为更优秀程序员的5个好习惯
  • HBase生产环境配置与使用优化不完全指南
  • co模块的前端实现
  • 【转】【支付 . 技术控】最全最强解析:支付宝系统架构内部剖析(架构图)...
  • php的引用
  • [deviceone开发]-do_Webview的基本示例
  • [译] 怎样写一个基础的编译器
  • ComponentOne 2017 V2版本正式发布
  • java 多线程基础, 我觉得还是有必要看看的
  • node-glob通配符
  • QQ浏览器x5内核的兼容性问题
  • springMvc学习笔记(2)
  • 分享几个不错的工具
  • 两列自适应布局方案整理
  • 前端工程化(Gulp、Webpack)-webpack
  • 如何利用MongoDB打造TOP榜小程序
  • 入门到放弃node系列之Hello Word篇
  • 双管齐下,VMware的容器新战略
  • 一个项目push到多个远程Git仓库
  • 因为阿里,他们成了“杭漂”
  • 用 Swift 编写面向协议的视图
  • 怎么把视频里的音乐提取出来
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • 容器镜像
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #微信小程序(布局、渲染层基础知识)
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (ibm)Java 语言的 XPath API
  • (Oracle)SQL优化技巧(一):分页查询
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (十一)图像的罗伯特梯度锐化
  • (转)PlayerPrefs在Windows下存到哪里去了?
  • **PHP分步表单提交思路(分页表单提交)
  • .NET DataGridView数据绑定说明
  • .NET 回调、接口回调、 委托
  • .NET 中什么样的类是可使用 await 异步等待的?
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .Net6使用WebSocket与前端进行通信
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .NET连接数据库方式