javascript判断一个二叉树是否是另一个二叉树的子树
时间: 2024-08-21 14:03:27 浏览: 28
在JavaScript中,判断一个二叉树是否是另一个二叉树的子树通常涉及到递归算法。你可以通过比较两个树的结构和节点值来进行。以下是基本的步骤:
1. **递归方法**:
- 对于每个待检查的树(称为`rootA`),你需要遍历另一棵树(称为`rootB`)的所有节点。
- 比较`rootA`的根节点和`rootB`的当前节点的值,如果相等,进入子树的递归判断。
- 如果`rootB`为空,说明已经找到了匹配的子树,返回`true`。
- 接着,递归地检查`rootA`的左子树和`rootB`的左子树,以及`rootA`的右子树和`rootB`的右子树。
- 如果任何一个分支不符合条件(即节点值不等或者某个子树为空而对应的子树非空),则返回`false`。
2. **代码示例** (简化版):
```javascript
function isSubtree(rootA, rootB) {
if (!rootA || !rootB) return !rootB; // 如果rootA为空,则rootB必须为空
return (
rootA.val === rootB.val &&
isSubtree(rootA.left, rootB.left) &&
isSubtree(rootA.right, rootB.right)
);
}
```
请注意,这个方法假设你有一个包含`val`, `left`, 和 `right`属性的二叉树节点类,并且所有节点都是唯一的。
相关问题
帮我用js 写一个二叉树遍历算法的代码
好的,以下是二叉树遍历算法的 JavaScript 代码:
// 定义二叉树节点
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// 二叉树遍历函数
function traverse(root) {
if (!root) return; // 如果根节点为空,直接返回
traverse(root.left); // 遍历左子树
console.log(root.val); // 输出当前节点的值
traverse(root.right); // 遍历右子树
}
// 测试代码
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
traverse(root); // 输出 4 2 5 1 6 3 7
用js和html、css写一个二叉树的深度优先遍历
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>二叉树深度优先遍历</title>
<style type="text/css">
.node {
height: 50px;
width: 50px;
background-color: #ccc;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
font-weight: bold;
color: #fff;
}
.tree {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-top: 50px;
}
.line {
height: 20px;
width: 1px;
background-color: #ccc;
position: absolute;
left: 50%;
transform: translateX(-50%);
z-index: -1;
}
.line.vertical {
height: 50px;
width: 20px;
transform: translate(-50%, -50%);
}
.line.horizontal {
height: 1px;
width: 50px;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="tree">
<div class="node" id="node-1">1</div>
<div class="line vertical" id="line-1"></div>
<div class="node" id="node-2">2</div>
<div class="line vertical" id="line-2"></div>
<div class="node" id="node-3">3</div>
<div class="line horizontal" id="line-3"></div>
<div class="line vertical" id="line-4"></div>
<div class="node" id="node-4">4</div>
<div class="line vertical" id="line-5"></div>
<div class="node" id="node-5">5</div>
<div class="line horizontal" id="line-6"></div>
<div class="line vertical" id="line-7"></div>
<div class="node" id="node-6">6</div>
<div class="line vertical" id="line-8"></div>
<div class="node" id="node-7">7</div>
<div class="line vertical" id="line-9"></div>
<div class="node" id="node-8">8</div>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
```
JS:
```javascript
const nodeList = document.querySelectorAll('.node');
const lineList = document.querySelectorAll('.line');
// 二叉树结构
const tree = {
value: 1,
left: {
value: 2,
left: {
value: 4,
left: null,
right: null
},
right: {
value: 5,
left: null,
right: null
}
},
right: {
value: 3,
left: {
value: 6,
left: null,
right: null
},
right: {
value: 7,
left: null,
right: {
value: 8,
left: null,
right: null
}
}
}
};
// 深度优先遍历
function dfs(node) {
if (node) {
console.log(node.value); // 打印节点的值
dfs(node.left); // 递归遍历左子树
dfs(node.right); // 递归遍历右子树
}
}
dfs(tree); // 从根节点开始遍历
// 显示遍历过程
let index = 0;
function dfsWithAnimation(node) {
if (node) {
setTimeout(() => {
nodeList[index].style.backgroundColor = '#ff0000'; // 用红色表示当前节点
if (node.left) {
// 左子树存在,画一条垂直线连接当前节点和左子节点
lineList[index * 2].style.backgroundColor = '#ff0000';
lineList[index * 2].style.height = '50px';
lineList[index * 2].style.transform = 'translate(-50%, 0)';
lineList[index * 2].style.zIndex = '1';
lineList[index * 2].style.transition = 'height 0.5s, transform 0.5s';
}
if (node.right) {
// 右子树存在,画一条垂直线连接当前节点和右子节点
lineList[index * 2 + 1].style.backgroundColor = '#ff0000';
lineList[index * 2 + 1].style.height = '50px';
lineList[index * 2 + 1].style.transform = 'translate(-50%, 0)';
lineList[index * 2 + 1].style.zIndex = '1';
lineList[index * 2 + 1].style.transition = 'height 0.5s, transform 0.5s';
}
index++;
dfsWithAnimation(node.left); // 递归遍历左子树
dfsWithAnimation(node.right); // 递归遍历右子树
nodeList[index - 1].style.backgroundColor = '#ccc'; // 当前节点遍历完成,恢复原来颜色
if (node.left) {
// 左子树遍历完成,恢复原来颜色
lineList[index * 2 - 2].style.backgroundColor = '#ccc';
lineList[index * 2 - 2].style.height = '20px';
lineList[index * 2 - 2].style.transform = 'translate(-50%, -50%)';
lineList[index * 2 - 2].style.zIndex = '-1';
lineList[index * 2 - 2].style.transition = 'height 0.5s, transform 0.5s';
}
if (node.right) {
// 右子树遍历完成,恢复原来颜色
lineList[index * 2 - 1].style.backgroundColor = '#ccc';
lineList[index * 2 - 1].style.height = '20px';
lineList[index * 2 - 1].style.transform = 'translate(-50%, -50%)';
lineList[index * 2 - 1].style.zIndex = '-1';
lineList[index * 2 - 1].style.transition = 'height 0.5s, transform 0.5s';
}
}, 500 * index);
}
}
dfsWithAnimation(tree); // 从根节点开始遍历
```