输入两棵二叉树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. Proteus仿真—51单片机实现AC信号测频、显示、双机通信

    文章目录 一.原理图部分 二.源码部分 单片机1 单片机2 在Proteus仿真软件里面使用STC89C52实现指定频率的AC信号的测频.显示.双机通信. 一.原理图部分 整体的电路图如示: DC-A ...

  2. ThreadPoolExecutor(线程池)的参数

    构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit u ...

  3. mac Charles抓包

    手机配置http代理 1.配置iPhone或Android 的wifi配置.首先保证Mac电脑和手机是在同一个局域网内. 2.设置手机wifi配置,在HTTP代理中选择手选代理,服务器填写Mac的IP ...

  4. 刷题-力扣-LCP 07. 传递信息

    LCP 07. 传递信息 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/chuan-di-xin-xi 著作权归领扣网络所有.商业转 ...

  5. React 性能调优记录(下篇),如何写高性能的代码

    react性能非常重要,性能优化可以说是衡量一个react程序员水平的重要标准. 减少你的渲染 这个大家都明白,只要是父组件中用了子组件,子组件就算没用prop也会进行依次渲染, 可以用pureCom ...

  6. tree命令出现乱码

    alias tree='tree --charset ASCII'就可以了

  7. Python中的变量以及变量的命名

    1.变量的定义 在 python 中,每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建 等号(=)用来给变量赋值 =左边是一个变量名 =右边是存储在变量中的值 变量名=值 变量定义之后,后续就 ...

  8. 基于Bootstrap v4.1.1 & Bootstrap-table-1.14.1实现数据瀑布流

    基于Bootstrap-table-1.14.1实现数据瀑布流 HTML代码 <div id="AvgWaitAndAvgTimeServiceTimeData_hall"& ...

  9. Docker下制作一个容器镜像

    操作过程描述: (1)先基于centos的镜像启动一个centos容器 (2)在这个容器中安装nginx (3)然后把这个已经安装了nginx的容器制作成一个docker的镜像 操作:docker c ...

  10. 20200713晚 noip14

    考场 很紧张,上午考太烂了 开场看到"影魔",想起以前看过(但没做),心态爆炸,咆哮时被 hkh diss 了 T1 一开始想建边跑最长路,每个点在记录一下 \(\min\{a\} ...