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 ...
随机推荐
- 常见web中间件漏洞(四)Tomcat漏洞
这部分好久没写了,继续更新web中间件漏洞思路整理(不复现) ,争取...整理完 前几篇指路链接: nginx: https://www.cnblogs.com/lcxblogs/p/13596239 ...
- ☕【Java技术指南】「Guava Collections」实战使用相关Guava不一般的集合框架
Google Guava Collections 使用介绍 简介 Google Guava Collections 是一个对 Java Collections Framework 增强和扩展的一个开源 ...
- S3C2440—2.裸机开发步骤及工具使用
文章目录 一.裸机开发步骤简介 1.在X86架构的Windows系统中 2.在X86架构的Ubuntu系统中 3.ARM裸机开发 二.soucre insight使用 1.sourec insight ...
- Windows下安装RocketMQ
目录 前言 环境 具体操作 下载 环境变量配置 启动 关闭 生产.消费实例 RocketMQ Console 前言 项目中用到了延迟消息队列,记录下一win10下rocketmq的安装 环境 win1 ...
- Docker创建Docker-Registry客户端docker-registry-frontend
docker-compose.yml version: '3.1' services: frontend: image: konradkleine/docker-registry-frontend:v ...
- Java全家桶的这些知识,不用学了
众所周知,Java 的知识体系繁冗复杂,但是有很多知识在实际工作中几乎没有人用. 很多人在学习过程中,却经常把有限的时间和精力花在了这些"没有用"的知识上,事倍功半. 下面我捋一捋 ...
- C# 调用DOS 命令
class NetWorkDeviceInfo { public static string GetDeviceInfo() { System.Diagnostics.Process p = new ...
- Javascript - Vue - vuex
vuex 这是一个与vue配套的公共数据管理工具,可以将一些需要共享的数据保存到vuex中,以此方便项目中的任何组件都可以从vuex中得到共享数据.cnpm i vuex -S 装包 读取数据 //在 ...
- NIO中的File
package nio; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files ...
- mac下用clion进行sdl2游戏开发de环境搭建
1. 故事背景 想从unity转unreal了,于是要使用c++进行开发.unreal引擎那么大,每次打开,我的小本都嗡嗡嗡的,想着不如用个轻量一些的引擎先开发吧,核心代码独立出来,到时候如果真要移植 ...