刷LeetCode刷到遍歷二叉樹的題目造寝,前序(題號144,中等)吭练,中序(題號94诫龙,中等),后序(題號145鲫咽,困難)签赃。遞歸比較簡單,但是題目建議可以用迭代分尸,三道題用簡單的遞歸也都可以通過锦聊,迭代的思路難一點。
JavaScript實現(xiàn)
前序-遞歸法:
/**
* 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) {
var res = [];
res = getNode(root);
return res;
};
var getNode = function(root){
if(root === null){
return [];
}
var arr = [];
arr = [root.val].concat(getNode(root.left));
arr = arr.concat(getNode(root.right));
return arr;
};
前序-迭代法:
/**
* 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) {
var cur = root;
var res = [];
var stack = [];
while(cur || stack.length!==0){
if(!cur){
cur = stack.pop();
}
res.push(cur.val);
if(cur.right){
stack.push(cur.right);
}
cur = cur.left;
}
return res;
};
中序-遞歸法:
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// 題目要求用迭代,先寫個遞歸看看
var inorderTraversal = function(root) {
var res = [];
res = getNode(root);
return res;
};
var getNode = function(root){
if(root === null){
return [];
}
if(root.left===null && root.right===null){
return [root.val];
}
var arr = [];
// 左中右材蛛,中序遍歷
arr = getNode(root.left).concat([root.val]);
arr = arr.concat(getNode(root.right));
return arr;
};
中序-迭代法:
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// 迭代
var inorderTraversal = function(root) {
if(root === null){
return [];
}
var res = [];
var stack = [];
var cur = root;
while(cur!==null || stack.length!==0){
if(cur !== null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
res.push(cur.val);
cur = cur.right;
}
}
return res;
};
后序-遞歸法:
/**
* 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) {
var res = [];
res = getNode(root);
return res;
};
var getNode = function(root){
if(root === null){
return [];
}
if(root.left===null && root.right===null){
return [root.val];
}
var arr = [];
arr = getNode(root.left).concat(getNode(root.right));
arr = arr.concat([root.val]);
return arr;
};
后序-迭代法:
/**
* 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 [];
}
var res = [];
var stack = [];
var cur = root;
while(cur || stack.length!==0){
if(cur){
stack.push(cur);
cur = cur.left;
}else{
// 檢查還有沒有右邊
if(stack[stack.length-1].right){
cur = stack[stack.length-1].right;
stack[stack.length-1].right = null;
}else{
cur = stack.pop();
res.push(cur.val);
cur = null;
}
}
}
return res;
};
上面遞歸的方法其實沒必要用兩個function的圆到,不過算了懶得改了怎抛。⊙康看得明白就行