144. 二叉树的前序遍历
原题链接: 144. 二叉树的前序遍历
递归法:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
// 递归
let res=[];
const preOrder=(root)=>{
if(!root){
return
}else{
res.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
preOrder(root);
return res;
};
迭代法: 用的是push()方法
var preorderTraversal = function(root) {
let res=[];
if(!root) return res;
const stack=[];
// 根节点入栈
stack.push(root);
while(stack.length){
const cur=stack.pop();
res.push(cur.val);
if(cur.right){
stack.push(cur.right);
}
if(cur.left){
stack.push(cur.left);
}
}
return res;
};
145. 二叉树的后序遍历
原题链接: 145. 二叉树的后序遍历
递归法:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var postorderTraversal = function(root) {
// 递归
let res=[];
const postOrder=(root)=>{
if(!root){
return
}else{
postOrder(root.left);
postOrder(root.right);
res.push(root.val);
}
}
postOrder(root);
return res;
};
迭代法: 用的是unshift()方法
var postorderTraversal = function(root) {
let res=[];
if(!root) return res;
const stack=[];
// 根节点入栈
stack.push(root);
while(stack.length){
const cur=stack.pop();
res.unshift(cur.val);
if(cur.left){
stack.push(cur.left);
}
if(cur.right){
stack.push(cur.right);
}
}
return res;
};
94. 二叉树的中序遍历
原题链接: 94. 二叉树的中序遍历
递归法:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
// 递归
let res=[];
const inorder=(root)=>{
if(!root){
return
}else{
inorder(root.left);
res.push(root.val);
inorder(root.right);
}
}
inorder(root);
return res;
};
迭代法:
var inorderTraversal = function(root) {
let res=[];
const stack=[];
// cur用于游标来不断向下搜索
let cur=root;
while(cur||stack.length){
// 寻找最左叶子结点,途径的结点全保存下来
while(cur){
//将途经的结点入栈
stack.push(cur);
// 搜索左孩子
cur=cur.left;
}
cur=stack.pop();
res.push(cur.val);
// 搜索右孩子
cur=cur.right;
}
return res;
};
102. 二叉树的层序遍历
原题链接: 102. 二叉树的层序遍历
递归法:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
//用一个变量记录当前所在的层 数组的下标为当前所在的层
const arr=[];
const order=(node,h)=>{
if(!node) return;
// 如果第一次进入该层 创建空数组
if(!arr[h]) arr[h]=[];
// 将节点值添加到该层数组
arr[h].push(node.val);
order(node.left,h+1);
order(node.right,h+1);
}
order(root,0);
return arr;
};
迭代法:
var levelOrder = function(root) {
//迭代法
let res=[],queue=[];
queue.push(root);
if(root===null){
return res;
}
while(queue.length!==0){
// 记录当前层级节点数
let length=queue.length;
//存放每一层的节点
let curLevel=[];
//遍历当前层级的结果
for(let i=0;i<length;i++){
// 将当前队列第一个节点移出 赋值给node
let node=queue.shift();
curLevel.push(node.val);
// 存放当前层下一层的节点
if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
//把每一层的结果放到结果数组
res.push(curLevel);
}
return res;
};
107. 二叉树的层序遍历 II
原题链接: 107. 二叉树的层序遍历 II
迭代法:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrderBottom = function(root) {
let res=[],queue=[];
queue.push(root);
if(!root) return res;
while(queue.length!==0){
let len=queue.length;
let curArr=[];
for(let i=0;i<len;i++){
let node=queue.shift();
curArr.push(node.val);
if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
//在存入res结果数组时 注意存入顺序
//res.push(curArr);
res.unshift(curArr);
}
// return res.reverse();
return res;
};