题目链接:
144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
题目描述:进行二叉树的前中后序递归遍历
解题思路:
二叉树的递归调用较简单只需调整访问节点的顺序即可,主要是了解前中后序中节点访问顺序,分别为先根中跟后跟即可。
代码实现:
前序递归遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Traversal(root, res);
return res;
}
public void Traversal(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}
res.add(node.val);
Traversal(node.left, res);
Traversal(node.right, res);
}
}
中序递归遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Traversal(root, res);
return res;
}
public void Traversal(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}
Traversal(node.left, res);
res.add(node.val);
Traversal(node.right, res);
}
}
后续递归遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Traversal(root, res);
return res;
}
public void Traversal(TreeNode node, List<Integer> res) {
if (node == null) {
return;
}
Traversal(node.left, res);
Traversal(node.right, res);
res.add(node.val);
}
}
题目链接:
144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
题目描述:
使用迭代法写出前中后序遍历
解题思路:
要实现迭代遍历二叉树,就是要理解递归调用就是每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中,因此我们就可以用栈来实现迭代遍历。
此处借用卡哥的过程效果动画
二叉树的前序输出循序为根左右,因此入栈顺序就是根-右左,因为每次出栈一个元素,因此左右孩子要相反入栈,才能实现出栈顺序为根左右。
二叉序的后序遍历与前序遍历思路一致只不过最后将结果集反转一下就可得到后序遍历
二叉序的中序遍历则要采用不同的思想,因为前序遍历要访问的元素和要处理的元素(也就是加入结果集)顺序是一致的,都是中间节点。所以可以只用栈就可解决。但中序遍历要一直访问左孩子知道叶子节点才能开始加入结果集,因此就需要使用指针帮助访问节点,栈则用来存储处理节点的顺序。
代码实现:
前序迭代遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
res.add(node.val);
if (node.right != null){
stack.push(node.right);
}
if (node.left != null){
stack.push(node.left);
}
}
return res;
}
}
中序迭代遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
TreeNode temp = root;
Stack<TreeNode> stack = new Stack<>();
while(temp != null|| !stack.isEmpty()){
if(temp != null){
stack.push(temp);
temp = temp.left;
}else{
temp = stack.pop();
res.add(temp.val);
temp = temp.right;
}
}
return res;
}
}
后序迭代遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
res.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
Collections.reverse(res);
return res;
}
}
题目链接:
144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
题目描述:
针对三种遍历方式,使用迭代法写出统一风格的代码!
解题思路:
这个思想是学习卡哥思想
我们以中序遍历为例,使用栈的话,无法同时解决访问节点(遍历节点)和处理节点(将元素放进结果集)不一致的情况。
那我们就将访问的节点放入栈中,把要处理的节点也放入栈中但是要做标记。
如何标记呢,就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。 这种方法也可以叫做标记法。
用我自己的理解是,将所有数据按照遍历的顺序反向入栈,但是因为有些是访问了但是没有加入结果集,因此我们需要在他之后入栈一个null作为标记等待下次处理,(其实就是每次只需要将当前访问的节点后加null)因为我们的入栈顺序时反向遍历,所以出栈顺序时正确的,所以处理出栈元素的顺序也是正确的也就是。
代码实现:
前序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
} else {
return res;
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
stack.push(node);
stack.push(null);
} else {
stack.pop();
node = stack.pop();
res.add(node.val);
}
}
return res;
}
}
中序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
}else{
return res;
}
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node != null){
stack.pop(); // 因为后面要进行加入右中左,不弹出的话中加入会导致重复
if(node.right != null){
stack.push(node.right);
}
stack.push(node);
stack.push(null);//访问了中但是没有处理加null辨识
if(node.left != null){
stack.push(node.left);
}
}else{
stack.pop();
node = stack.pop();
res.add(node.val);
}
}
return res;
}
}
后序遍历
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
if (root != null) {
stack.push(root);
} else {
return res;
}
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
stack.push(node);
stack.push(null);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
} else {
stack.pop();
node = stack.pop();
res.add(node.val);
}
}
return res;
}
}