标签:root span 队列 arch color dfs length 广度优先 logs
1. 二叉树的深度优先遍历,使用栈Stack,
function DFS(root){ var stack = []; stack.push(root); var node = null; while(stack.length){ node = stack.pop(); //visit node.data; if(node.right){ stack.push(node.right); } if(node.left){ stack.push(node.left); } } }
2. 二叉树的广度优先遍历,使用队列Queue
function BFS(root){ var queue = []; queue.push(root); var node = null; while(queue.length){ node = queue.shift(); //visit node.data if(node.left){ queue.push(node.left); } if(node.right){ queue.push(node.right); } } }
标签:root span 队列 arch color dfs length 广度优先 logs
原文地址:http://www.cnblogs.com/mengff/p/6838900.html