1.中序遍历
https://leetcode-cn.com/problems/binary-tree-inorder-traversal/submissions/
1.1递归
class Solution {
List result;
public List inorderTraversal(TreeNode root) {
result =new ArrayList<>();
if(root==null) return result;
getMyFixGroup(root);
return result;
}
private void getMyFixGroup(TreeNode root) {
//加入左边
if (root.left != null) {
getMyFixGroup( root.left );
}
//加入根
result.add( root.val );
//加入右边
if (root.right != null) {
getMyFixGroup( root.right );
}
}
}
1.2非递归
class Solution {
public List inorderTraversal(TreeNode root) {
List res = new ArrayList<>();
Stack wait = new Stack<>();
boolean isRight = false;
while(!wait.empty()||root!=null){ //1.当当前节点或者栈内的节点不为空的时候,循环继续
if(root!=null){ //2.节点不为null
if(!isRight&&root.left!=null){//3节点的左子树不为null且不是栈内取出的节点
wait.add(root); //4.根节点加入栈
root = root.left; //5.当前节点置为根的左节点
}else{
res.add(root.val); //6.左节点为null,则根加入结果,并置当前节点为根的右节点
root = root.right;
}
isRight = false; //7.不是从栈内取出的标志位
}else{
root = wait.pop(); //8.当前的节点为空,但栈不为空,从栈内弹出一个节点
isRight = true; //9,栈内取出的标志位为true
}
}
return res;
}
}
剩下的遍历和中序遍历类似
2.前序遍历
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
2.1递归
class Solution {
List result;
public List preorderTraversal(TreeNode root) {
result = new ArrayList<>();
checkMyNode(root);
return result;
}
private void checkMyNode(TreeNode root) {
if(root == null)return;
result.add(root.val);
checkMyNode(root.left);
checkMyNode(root.right);
}
}
2.2非递归
class Solution {
public List preorderTraversal(TreeNode root) {
Stack wait = new Stack<>();
List res = new ArrayList<>();
//1.当当前节点或者栈内的节点不为空的时候,循环继续
while(root!=null||!wait.isEmpty()){
if(root!=null){
//2.根节点加入结果
res.add(root.val);
//3节点的右子树不为null
if(root.right!=null){
//4.加入待处理节点
wait.add(root.right);
}
//5.根节点更新为根的左节点
root = root.left;
}else{
//6.如果当前的节点为null,从栈节点返回
root = wait.pop();
}
}
return res;
}
}
3.后序遍历
https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
3.1递归
class Solution {
List result;
public List postorderTraversal(TreeNode root) {
result = new ArrayList<>();
if(root != null) {
checkMyNode(root);
}
return result;
}
private void checkMyNode(TreeNode root) {
if(root == null)return;
checkMyNode(root.left);
checkMyNode(root.right);
result.add(root.val);
}
}
3.2非递归
class Solution {
public List postorderTraversal(TreeNode root) {
//1.待处理的节点
Stack wait = new Stack<>();
//2.该节点左右是否已经处理的标志
Stack flag = new Stack<>();
//3.结果
List res = new ArrayList<>();
boolean isDeal = false;
while(root!=null||!wait.isEmpty()){
if(root!=null){
if(!isDeal){
//1.当前节点未处理,且左右节点的各种情况
if(root.left == null&&root.right == null){
res.add(root.val);
root = null;
}else if(root.left==null&&root.right!=null){
wait.add(root);
flag.add(true);
root = root.right;
}else if(root.left!=null&&root.right==null){
wait.add(root);
flag.add(true);
root = root.left;
}else if(root.left!=null&&root.right!=null){
wait.add(root);
flag.add(true);
wait.add(root.right);
flag.add(false);
root = root.left;
}
isDeal = false;
}else{
//当前节点的左右节点都已经处理过了,直接接入结果
res.add(root.val);
if(!wait.empty()){
root = wait.pop();
isDeal = flag.pop();
}else{
root = null;
}
}
}else{
//从待处理的栈中弹出结果
root = wait.pop();
isDeal = flag.pop();
}
}
return res;
}
}