从根出发,尽可能深的搜索树的节点
技巧:
从根出发,优先访问离根节点最近的节点
技巧:
//树
const tree = {
val: 'a',
children: [
{
val: 'b',
children: [
{ val: 'c', children: [] },
{ val: 'd', children: [] },
]
},
{
val: 'e',
children: [
{ val: 'f', children: [] },
{ val: 'g', children: [] },
]
}
]
}
//深度优先遍历
const fun1 = (root)=>{
console.log(root.val);
root.children.forEach(fun1);
}
fun1(tree)
//广度优先便利
const fun2 = (root) => {
const arr=[root]
while (arr.length > 0) {
var item = arr.shift()
console.log(item.val);
item.children.forEach(item => {
arr.push(item)
})
}
}
fun2(tree)
const tree = {
val: '1',
left:{
val:'2',
left: { val: '4', left: null, right: null },
right: { val: '5', left: null, right: null },
},
right: {
val: '3',
left: { val: '6', left: null, right: null },
right: { val: '7', left: null, right: null },
}
}
//递归
var preorderTraversal = function (root) {
let arr=[]
var fun = (node) => {
if (node) {
//先根节点
arr.push(node.val)
//遍历左子树
fun(node.left)
//遍历右子树
fun(node.right)
}
}
fun(root)
return arr
}
console.log(preorderTraversal(tree));//['1', '2', '4', '5', '3', '6', '7']
//非递归
var preorderTraversalTwo=function(root){
if (!root) return [];
let arr = []
//根节点入栈
let stack = [root]
while (stack.length) {
let current = stack.pop()
arr.push(current.val)
current.right ? stack.push(current.right) :null
current.left ? stack.push(current.left):null
}
return arr
}
console.log(preorderTraversalTwo(tree));
const tree = {
val: '1',
left:{
val:'2',
left: { val: '4', left: null, right: null },
right: { val: '5', left: null, right: null },
},
right: {
val: '3',
left: { val: '6', left: null, right: null },
right: { val: '7', left: null, right: null },
}
}
//递归
var inorderTraversal = function (root) {
let arr=[]
var fun = (node) => {
if (node) {
//遍历左子树
fun(node.left)
//根节点
arr.push(node.val)
//遍历右子树
fun(node.right)
}
}
fun(root)
return arr
}
console.log(inorderTraversal(tree));
//非递归
var inorderTraversalTwo=function(root){
if (!root) return [];
let arr = []
let stack = []
let o=root
while (stack.length || o) {
while (o) {
stack.push(o)
o=o.left
}
const n = stack.pop();
arr.push(n.val)
o=n.right
}
return arr
}
console.log(inorderTraversalTwo(tree));//['4', '2', '5','1', '6', '3','7']
const tree = {
val: '1',
left:{
val:'2',
left: { val: '4', left: null, right: null },
right: { val: '5', left: null, right: null },
},
right: {
val: '3',
left: { val: '6', left: null, right: null },
right: { val: '7', left: null, right: null },
}
}
//递归
var postorderTraversal = function (root) {
let arr=[]
var fun = (node) => {
if (node) {
//遍历左子树
fun(node.left)
//遍历右子树
fun(node.right)
//根节点
arr.push(node.val)
}
}
fun(root)
return arr
}
console.log(postorderTraversal(tree));['4', '5', '2','6', '7', '3','1']
//非递归
var postorderTraversalTwo=function(root){
if (!root) return [];
let arr = []
//根节点入栈
let stack = [root]
while (stack.length) {
let current = stack.pop()
arr.unshift(current.val)
current.left ? stack.push(current.left) : null
current.right ? stack.push(current.right) : null
}
return arr
}
console.log(postorderTraversalTwo(tree));