var tree = {
value: 1,
left: {
value: 2,
left: {
value: 4
}
},
right: {
value: 3,
left: {
value: 5,
left: {
value: 7
},
right: {
value: 8
}
},
right: {
value: 6
}
}
}
var preOrder = function (root, array = []) {
if (root) {
preOrder(root.left, array);
array.push(root.value);
preOrder(root.right, array);
}
return array;
};
function inOrder(root) {
if (root) {
inOrder(root.left);
console.log(root.value);
inOrder(root.right);
}
}
function postOrder(root) {
if (root) {
postOrder(root.left);
postOrder(root.right);
console.log(root.value);
}
}
var preOrderNew = function(node) {
const result = [];
const stack = []
stack.push(node)
while(stack.length > 0) {
const current = stack.pop();
result.push(current.value);
if(current.right) stack.push(current.right)
if(current.left) stack.push(current.left)
}
return result;
}
var levelOrderTraversal = function(node) {
const result = [];
const que = []
if (node) {
que.push(node)
while(que.length !== 0) {
const current = que.shift()
result.push(current.value)
if(current.left) que.push(current.left)
if(current.right) que.push(current.right)
}
return result;
}
}
var inorderTraversal = function (root) {
const result = [];
const stack = [];
let current = root;
while (current || stack.length > 0) {
while (current) {
stack.push(current);
current = current.left;
}
current = stack.pop();
result.push(current.value);
current = current.right;
}
return result;
};
console.log(inorderTraversal(tree));
var inorderTraversal = function(root) {
const result = [];
if(!root) return [];
const stack = [root];
while(stack.length > 0){
while(root.left){
root = root.left;
stack.push(root);
}
let cur = stack.pop();
result.push(cur.value);
if(cur.right){
root = cur.right;
stack.push(root);
}
}
return result;
};
const dsa = {
value: 1,
left: {
value: 2,
left: {
value: 4,
},
right: {
value: 5,
left: {
value: 6,
},
right: {
value: 7
}
}
},
right: {
value: 3
}
}
console.log('inorderTraversal', inorderTraversal(dsa));
function inOrderTest(root) {
const result = [];
if (!root) return []
const stack = [root];
while(stack.length > 0) {
while(root.left) {
root = root.left;
stack.push(root);
}
let cur = stack.pop();
result.push(cur.value);
if (cur.right) {
root = cur.right
stack.push(root);
}
}
return result;
}
console.log(inOrderTest(dsa));
var postorderTraversal = function (root) {
const result = [];
const stack = [];
let last = null;
let current = root;
while (current || stack.length > 0) {
while (current) {
stack.push(current);
current = current.left;
}
current = stack[stack.length - 1];
if (!current.right || current.right == last) {
current = stack.pop();
result.push(current.value);
last = current;
current = null;
} else {
current = current.right;
}
}
return result;
}
var postorderTraversal22 = function(root) {
let stack = [], res = []
root && stack.push(root)
while(stack.length > 0) {
let cur = stack.pop()
res.push(cur.value)
cur.left && stack.push(cur.left)
cur.right && stack.push(cur.right)
}
return res.reverse()
};
console.log(99, postorderTraversal(dsa), postorderTraversal22(dsa));
console