二叉树前序遍历 leetcode114node
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[]} */ var preorderTraversal = function(root) { if(root){ return [root.val,...preorderTraversal(root.left),...preorderTraversal(root.right)] }else{ return [] } };
二叉树中序遍历 leetcode94数组
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[]} */ var arr=[]//结果 var inorderTraversal = function(root) { //递归终止条件 if(root==null) return arr if(root){ return [...inorderTraversal(root.left),root.val,...inorderTraversal(root.right)] } };
二叉树后序遍历 leetcode145函数
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[]} */ var postorderTraversal = function(root) { if(root==null) return[]; if(root) return [...postorderTraversal(root.left),...postorderTraversal(root.right),root.val] };
二叉树层序遍历(由上至下)leetcode102post
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[][]} */ var levelOrder = function(root) { if (!root) return [] let res = []//保存结果 let queue = [root] while (queue.length) { // 没有节点在列,就是遍历完毕 let subRes = [] const len = queue.length // 当前层的节点数目 for (let i = 0; i < len; i++) { // 遍历当前层的节点 let cur = queue.shift() // 出列 subRes.push(cur.val) // 填充subRes数组 if (cur.left) queue.push(cur.left) // 下层节点入列 if (cur.right) queue.push(cur.right) } res.push(subRes) } return res };
二叉树的层序遍历(下至上)leetcode107this
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {number[][]} */ var levelOrder = function(root) { if (!root) return [] let res = []//保存结果 let queue = [root] while (queue.length) { // 没有节点在列,就是遍历完毕 let subRes = [] const len = queue.length // 当前层的节点数目 for (let i = 0; i < len; i++) { // 遍历当前层的节点 let cur = queue.shift() // 出列 subRes.push(cur.val) // 填充subRes数组 if (cur.left) queue.push(cur.left) // 下层节点入列 if (cur.right) queue.push(cur.right) } res.push(subRes) } return res };
N插树的前序遍历 leetcode589code
/** * // Definition for a Node. * function Node(val, children) { * this.val = val; * this.children = children; * }; */ /** * @param {Node} root * @return {number[]} */ var preorder = function(root) { if(root==null) return []//判空 var childs=[]//结果集 recusion(root);//执行函数 return childs;//返回结果集 //函数定义 function recusion(root){ if(root==null) return var len=root.children.length childs.push(root.val) for(let i=0;i<len;i++){ recusion(root.children[i]) } } };
N插树的后序遍历 leetcode590递归
/** * // Definition for a Node. * function Node(val,children) { * this.val = val; * this.children = children; * }; */ /** * @param {Node} root * @return {number[]} */ var postorder = function(root) { if(root==null) return [] var res=[]//结果集 gets(root)//执行函数 return res//返回结果 //函数定义 function gets(root){ if(root==null) return var len=root.children.length//子节点数量 for(var i=0;i<len;i++){//遍历子节点 gets(root.children[i])//深度 } res.push(root.val)//从叶子节点开始存储 } };
N插树的层序遍历 leetcode429leetcode
/** * // Definition for a Node. * function Node(val,children) { * this.val = val; * this.children = children; * }; */ /** * @param {Node} root * @return {number[][]} */ var levelOrder = function(root) { if(root==null) return [] var res=[]//结果集 var queue=[root]//先放入树的根节点 while(queue.length){ var subarr=[]//保存当前层的节点 const len=queue.length//当前层的长度 for(let i=0;i<len;i++){//只遍历当前层 var curr=queue.shift()//出列 subarr.push(curr.val)//放进subarr if(curr.children!=null){//存在子节点 queue.push(...curr.children)//当前层的下一层节点 } } res.push(subarr) } return res };