深度优先搜索即 depth first search, 简称 DFS
从根节点触发, 沿着路径, 达到最大层次的节点, 然后回退到上一节点, 继续搜索
有前序, 中序, 后序三种方式
前序遍历
根左右
a b c d e f g h i j
// 递归方式
public void firstRecursion(TreeNode root){
if (root != null) {
System.out.println(root.val);
firstRecursion(root.left);
firstRecursion(root.right);
}
}
中序遍历
左根右
c b e d a g f i h j
后序遍历
左右根
c e d b g i j h f a