输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

  

<?php
class TreeNode {
private $val;
private $left;
private $right; public function __construct($val=null, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
} /**
* @param $x : val
* @param $t TreeNode or: null
*/
public function insert($x) {
if ($this->val ===null) {
$this->val = $x;
}
if ($x < $this->val) {
$this->left = is_null($this->left) ? new self($x) :
$this->left->insert($x);
} else if ($x > $this->val) {
$this->right = is_null($this->right) ? new self($x):
$this->right->insert($x);
}
/* else x is in the tree already; we'll do nothing */
return $this;
} /**
* 先序遍历
* @param $callback
*/
public function traversePre($callback) {
if (is_null($this->val)) {
return;
}
call_user_func($callback, $this->val);
if (!is_null($this->left)) {
$this->left->traversePre($callback);
}
if (!is_null($this->right)) {
$this->right->traversePre($callback);
}
} /**
* 中序遍历
* @param $callback
*/
public function traverseMid($callback) {
if (is_null($this->val)) {
return;
}
if (!is_null($this->left)) {
$this->left->traverseMid($callback);
}
call_user_func($callback, $this->val);
if (!is_null($this->right)) {
$this->right->traverseMid($callback);
}
} /**
* @return null
*/
public function makeEmpty() {
if ($this->left !== null) {
$this->left->makeEmpty();
unset($this->left);
}
if ($this->right !== null) {
$this->right->makeEmpty();
unset($this->right);
}
$this->val = null;
return null;
} /**
* fixme: find(7) => find(11)?
* @return null
*/
public function find($x) {
if ($this->val===null) {
return null;
}
return ($x < $this->val) ? (is_null($this->left) ? null : $this->left->find($x)) :
($x > $this->val) ? (is_null($this->right) ? null: $this->right->find($x)) : $this;
} /**
* 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
* @param $pRoot1
* @param $pRoot2
* @return bool
*/
public static function HasSubtree($pRoot1, $pRoot2) {
if ($pRoot2 === null || $pRoot1===null) {
return false;
}
if ($pRoot1->val===$pRoot2->val) {
return self::isSubTree($pRoot1, $pRoot2) ||
self::isSubTree($pRoot1->left, $pRoot2) || self::isSubTree($pRoot1->right, $pRoot2);
}
return false;
} private static function isSubTree($root1, $root2) {
if ($root2===null) {return true;}
if ($root1 === null) {return false;}
if ($root1->val === $root2->val) {
return self::isSubTree($root1->left, $root2->left) && self::isSubTree($root1->right, $root2->right);
}
return false;
} }

  

test:

/**
8
/ \
6 10
/ \ / \
5 7 9 11
*/ $tree = new TreeNode(); $a = [8,6,10,5,7,9,11];
array_walk($a, function($value, $index, $tree) {
$tree->insert($value);
}, $tree); $tree->traversePre(function($elem) {echo $elem.',';});
echo PHP_EOL;
$tree->traverseMid(function($elem) {echo $elem.',';});
echo PHP_EOL;
var_dump($tree->find(7));
// $tree->makeEmpty();

  

  

PHP 一个树为另一棵树的子结构 [TO BE CONTINUED]的更多相关文章

  1. [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树

    将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...

  2. 判断一棵树是否为二叉搜索树(二叉排序树) python

    输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...

  3. 011-数据结构-树形结构-B+树[mysql应用]、B*树

    一.B+树概述 B+树是B树的变种,有着比B树更高的查询效率. 一棵 B+ 树需要满足以下条件: 节点的子树数和关键字数相同(B 树是关键字数比子树数少一) 节点的关键字表示的是子树中的最大数,在子树 ...

  4. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  5. 小希的迷宫(MST单棵树判断法则)

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. 2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup

    2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup 下载下来的zip解压得到两个jpg图片,在Kali中使用binwalk查看文件类型如下图: 有两个发现: 1111.jpg 隐藏了一 ...

  7. bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...

  8. 如何打印一棵树(Java)

    1.有一棵多叉树,将它打印出来. import java.util.LinkedList; /** * 需求:按层打印一棵树 * 说明:树是保存在一个链表中 * created by wangjunf ...

  9. Codeforces - 828E DNA Evolution —— 很多棵树状数组

    题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...

随机推荐

  1. Docker运行PostgreSQL

    docker-compose.yml version: '3.1' services: db: image: postgres restart: always ports: - 5432:5432 e ...

  2. 接口和包--Java学习笔记

    接口 定义及基础用法 interface定义:没有字段的抽象类 interface person{ void hello(); String getName(); } /*接口本质上就是抽象类 abs ...

  3. springmvc学习日志四

    一.回顾 1.文件上传 1.1引入fileupload的jar包 1.2在springmvc的配置文件中引入CommonsMutilpartResolver文件上传解析器 1.3在控制层在写入代码 2 ...

  4. 【springcloud】API Gateway 的路由和过滤(Zuul--1)

    转自:https://blog.csdn.net/pengjunlee/article/details/87084646 Zuul是什么? API Gateway 是随着微服务(Microservic ...

  5. etcd raft 处理流程图系列3-wal的存储和运行

    存储和节点的创建 raftexample中的存储其实有两种,一个是通过raft.NewMemoryStorage()进行创建的raft.raftStorage,关联到单个raft节点,另一个是通过ne ...

  6. OpenCV 传统分割测试

    github官网源文件:https://github.com/opencv/opencv/tree/master/samples/python 最好是先克隆整个仓库下来,再测试里面的:floodfil ...

  7. Ubuntu 系统安装、配置

    windows下制作安装U盘 使用工具:Universal USB Installer ubuntu下制作安装U盘 使用工具:Startup Disk Creator(自带) 选择国内源:Switch ...

  8. os.read

    #-*-coding:utf-8-*-__author__ = "logan.xu"import oscmd_res=os.popen("ls").read() ...

  9. 手撕LRU缓存

    面试官:来了,老弟,LRU缓存实现一下? 我:直接LinkedHashMap就好了. 面试官:不要用现有的实现,自己实现一个. 我:..... 面试官:回去等消息吧.... 大家好,我是程序员学长,今 ...

  10. 基于源码编译的lnmp架构实现论坛的搭建及memcache的应用

    系统环境: RHEL6 x86-64 selinux and iptables disabled LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构 Linux是一类 ...