力扣、144
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
 
// 递归遍历
var preorderTraversal = function(root){
	let arr = [];
	var fun = (node)=>{
		if(node){
			//先根节点:
			arr.push(node.val);
			fun(node.left);//遍历左子树
			fun(node(right);//遍历右子树
		}
	}
	fun(root)
	return arr;
}
 
// 栈的形式
var preorderTraversal = function(root){
	if( !root) return [];
	let arr = [];
	//根节点入栈
	let stack = [root];
	while ( stack.length){
		//出栈
		let o = stack.pop();
		arr.push(o.val)
		o.right && stack.push(o.right);//先推右边
		o.left && stack.push(o.left);//再推左边
	}
	return arr;
}
                


















