java非递归的后续遍历_用递归非递归的方式分别实现二叉树的先序、中序、后序遍历(java实现)...

首先约定:先序:根,左,右  中序:左,根,右  后序:左,右,根

递归实现

递归实现中,总是会经过一个节点三次,所以先序中序和后序的唯一区别就是打印的时机不同

public class Node{//这是Node的结构 public int value; public Node left; public Node right; public Node(int data){ this.value = data; }

} public void preOdferRecur(Node head){ if (head == null) { return; } System.out.print(head.value + " ");//先序遍历,打印放在第一行 preOdferRecur(head.left); preOdferRecur(head.right); } public void inOdferRecur(Node head){ if (head == null) { return; } inOdferRecur(head.left); System.out.print(head.value + " ");//中序遍历,打印放在第二行 inOdferRecur(head.right); } public void posOdferRecur(Node head){ if (head == null) { return; } posOdferRecur(head.left); posOdferRecur(head.right); System.out.print(head.value + " ");//后序遍历,打印放在第三行 }

非递归实现

非递归先序遍历

思路:申请一个栈,压入头结点,然后弹出栈节点,打印出来,再将弹出节点的右孩子压入,左孩子压入,不断重复这个过程,直到栈为空

public void preOrderUnRecur(Node head){ if (head!=null){ Stack stack=new Stack(); stack.add(head); while(!stack.isEmpty()){ head=stack.pop(); System.out.printf(head.value+" "); if (head.right!=null){ stack.push(head.right); } if (head.left!=null){ stack.push(head.left); } } } }

非递归中序遍历

思路:中序是左,根,右,因此考虑将左边界都压入栈,直到节点为空,则从栈中拿出一个打印,当前节点右移,若当前节点不为空,则压入栈,当前节点为左

申请一个栈,记为stack。初始时,令变量cur=head。

先把cur节点压入栈,对以cur节点为头结点的子树来说,依次把左边界压入栈中,即不停地令cur=cur.left,然后重复步骤2

直到cur为空,此时从stack中弹出一个节点,记为node。打印node,并让cur=node.right,然后持续重复步骤2

当stack为空且cur为空,这个过程停止

`public void inOrderUnRecur(Node head){ if(head!=null){ Stack stack=new Stack(); while (!stack.isEmpty()||head!=null){ if (head!=null){ stack.push(head); head=head.left; } else { head=stack.pop(); System.out.println(head.value+" "); head =head.right; } } } }

非递归后序遍历

思路:后序遍历是左,右,中,先序是中,左,右,将中左右变成中右左,在建立个栈将中右左压入,弹出即是后序遍历的次序

public void posOrderUnRecur(Node head){ if (head!=null){ Stack stack1=new Stack(); Stack stack2=new Stack(); stack1.add(head); while(!stack1.isEmpty()){ head=stack1.pop(); stack2.push(head); if (head.left!=null){ stack1.push(head.left); } if (head.right!=null){ stack1.push(head.right); } } while(!stack2.isEmpty()){ System.out.printf(stack2.pop().value+" "); } } }

文章来源: segmentfault.com,作者:Java攻城师,版权归原作者所有,如需转载,请联系作者。

原文链接:segmentfault.com/a/1190000038474043

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
``` #include<stdio.h> #include<stdlib.h> //定义二叉树结构体 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; //创建节点函数 struct TreeNode *createNode(int data) { struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } //插入节点函数 struct TreeNode *insert(struct TreeNode *root, int data) { if (root == NULL) { return createNode(data); } else if (data <= root->data) { root->left = insert(root->left, data); } else { root->right = insert(root->right, data); } return root; } //递归先序遍历函数 void preOrder(struct TreeNode *root) { if (root == NULL) { return; } printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } //递归中序遍历函数 void inOrder(struct TreeNode *root) { if (root == NULL) { return; } inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } //递归后序遍历函数 void postOrder(struct TreeNode *root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } //非递归层次遍历函数 void levelOrder(struct TreeNode *root) { if (root == NULL) { return; } struct TreeNode *queue[100]; int front = 0; int rear = 0; queue[rear] = root; rear++; while (front <= rear) { struct TreeNode *current = queue[front]; front++; printf("%d ", current->data); if (current->left != NULL) { queue[rear] = current->left; rear++; } if (current->right != NULL) { queue[rear] = current->right; rear++; } } } int main() { struct TreeNode *root = NULL; root = insert(root, 5); root = insert(root, 3); root = insert(root, 7); root = insert(root, 2); root = insert(root, 4); root = insert(root, 6); root = insert(root, 8); printf("递归先序遍历:"); preOrder(root); printf("\n"); printf("递归中序遍历:"); inOrder(root); printf("\n"); printf("递归后序遍历:"); postOrder(root); printf("\n"); printf("非递归层次遍历:"); levelOrder(root); printf("\n"); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值