PHP 一个树为另一棵树的子结构 [TO BE CONTINUED]
输入两棵二叉树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]的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- 判断一棵树是否为二叉搜索树(二叉排序树) python
输入一棵树,判断这棵树是否为二叉搜索树.首先要知道什么是排序二叉树,二叉排序树是这样定义的,二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的 ...
- 011-数据结构-树形结构-B+树[mysql应用]、B*树
一.B+树概述 B+树是B树的变种,有着比B树更高的查询效率. 一棵 B+ 树需要满足以下条件: 节点的子树数和关键字数相同(B 树是关键字数比子树数少一) 节点的关键字表示的是子树中的最大数,在子树 ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- 小希的迷宫(MST单棵树判断法则)
小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup
2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup 下载下来的zip解压得到两个jpg图片,在Kali中使用binwalk查看文件类型如下图: 有两个发现: 1111.jpg 隐藏了一 ...
- bzoj 2816: [ZJOI2012]网络 (LCT 建多棵树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2816 题面: http://www.lydsy.com/JudgeOnline/upload ...
- 如何打印一棵树(Java)
1.有一棵多叉树,将它打印出来. import java.util.LinkedList; /** * 需求:按层打印一棵树 * 说明:树是保存在一个链表中 * created by wangjunf ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
随机推荐
- CVE-2021-1732 Windows 本地权限提升漏洞 EXP 下载
漏洞简介 2021年2月10日,微软修复了一个Windows本地权限提升漏洞,漏洞编号为 CVE-2021-1732 ,本地攻击者可以利用该漏洞将权限提升为 System ,目前EXP已公开. 影响范 ...
- ffmpeg第2篇:简单滤镜与复杂滤镜的区别
在ffmpeg的滤镜中,有简单滤镜(simple filter)和复杂滤镜(complex filter)两种. 使用简单滤镜时,用-vf选项,使用复杂滤镜时,使用-filter_complex或-l ...
- node 报错 throw er; // Unhandled 'error' event 解决办法
node 报错 Starting child process with 'node web.js' events.js:183 throw er; // Unhandled 'error' event ...
- C# 不是异步的方法中获取异步的结果
var waiter = HP.UtilsLib.TaskAwaiterHelper.GetTaskAwaiter( async () => { return await feedBack(ve ...
- UWP AppConnection.
https://www.cnblogs.com/manupstairs/p/14582794.html
- 【java web】过滤器filter
一.过滤器简介 过滤器filter依赖于servlet容器 所谓过滤器顾名思义是用来过滤的,Java的过滤器能够为我们提供系统级别的过滤,也就是说,能过滤所有的web请求, 这一点,是拦截器无法做到的 ...
- [ASP.NET MVC]@Scripts.Render、@Styles.Render的使用
一.配置BundleConfig.cs文件 1.首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件 2.BundleConfig就是一个微软新加的 一个打 ...
- 关于servlet中要写初始化逻辑应该重载有参还是无参的init
关于开发者在写初始化逻辑的时候,应该选用的哪个init方法@author mzy 在查看servlet的源码的时候,因为servlet是一个接口使用较麻烦: 所以我们使用它的实现类:GenericSe ...
- 从零开始实现简单 RPC 框架 6:网络通信之 Netty
网络通信的开发,就涉及到一些开发框架:Java NIO.Netty.Mina 等等. 理论上来说,类似于序列化器,可以为其定义一套统一的接口,让不同类型的框架实现,事实上,Dubbo 就是这么干的. ...
- SQL语句之高级使用
1.select top select top 用于规定要返回的数据的数目 注意:并非所有的数据库系统都支持 SELECT TOP 语句. MySQL 支持 LIMIT 语句来选取指定的条数数据, ...