Java 二叉搜索树(BST)三种遍历方式的模板
1.先序遍历
if(root == null) {
return;
}
执行操作
dfs(root.left);
dfs(root.right);
2.中序遍历
if(root == null) {
return;
}
dfs(root.left);
执行操作
dfs(root.right);
3.后序遍历
if(root == null) {
return;
}
dfs(root.left);
dfs(root.right);
执行操作
总结:遇到二叉搜索树,应该想到:二叉搜索树(BST)的中序遍历是有序的,这是解决所有二叉搜索树的关键。二叉树的先序遍历、中序遍历、后序遍历方式的区别在于把【执行操作】放在两个递归函数的哪个位置。