二叉树的前、中、后序遍历
题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html
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) {
const res = [];
const preOrder = function(root){
if(root == null) return null;
res.push(root.val);
preOrder(root.left);
preOrder(root.right);
};
preOrder(root);
return res;
};
方法二:迭代法
/**
* 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) {
if(root === null) return [];
let res = [];
let stack = [root];
while(stack.length){
let cur = stack.pop();
res.push(cur.val);
if(cur.right) stack.push(cur.right);
if(cur.left) stack.push(cur.left);
}
return res;
};
注意:
1.这里cur使用的是pop,而不是shift,这里要与层序遍历区分开,层序遍历是用队列来模拟的,因此使用的是shift,取第一个元素;前序遍历是用栈来模拟的,因此使用的是pop。
2.因为使用的是栈,所以要按照中右左的顺序压入栈,这样出栈的时候才是中左右。
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 = [];
let postOrder = function(root){
if(root == null) return null;
postOrder(root.left);
postOrder(root.right);
res.push(root.val);
};
postOrder(root);
return res;
};
方法二:迭代
/**
* 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) {
if(root === null) return [];
let res = [];
let stack = [root];
while(stack.length){
let cur = stack.pop();
res.push(cur.val);
if(cur.left) stack.push(cur.left);
if(cur.right) stack.push(cur.right);
}
return res.reverse();
};
注意:
1.后序的迭代和前序的迭代很像,但是也有一些细节改动。
前序的迭代遍历是先push右再push左,正常是中左右(中不用考虑,因为中会先pop出来),由于是栈结构,所以需要先push右再push左,这样出栈顺序就是中左右了。
同理,后序遍历正常是左右中,因此先push左再push右,出栈顺序就是中右左,最后再reverse数组,就是左右中了。
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) {
const res = [];
const inOrder = function(root){
if(root == null) return null;
inOrder(root.left);
res.push(root.val);
inOrder(root.right);
};
inOrder(root);
return res;
};
方法二:迭代
/**
* 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 = [];
let stack = [];
while(root){
stack.push(root);
root = root.left;
}
while(stack.length){
let cur = stack.pop();
res.push(cur.val);
cur = cur.right;
while(cur){
stack.push(cur);
cur = cur.left;
}
}
return res;
};