二叉树先序遍历和中序遍历的非递归算法

二叉树的递归遍历算法很简单,而非递归遍历算法中的先序和中序遍历相比于后序遍历更简单一点,在这先实现先序和中序遍历的非递归算法。

先序遍历代码如下:

public static void preOrder(TreeNode root) {
		TreeNode current = root;
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while(current!=null || !stack.isEmpty()) {
			if(current!=null) {
				System.out.println(current.val);
				stack.push(current);
				current = current.left;
			}
			else {
				current = stack.pop();
				current = current.right;
			}
		}
	}

中序遍历代码如下:

public static void inOrder(TreeNode root) {
		TreeNode current = root;
		Stack<TreeNode> stack = new Stack<TreeNode>();
		while(current!=null || !stack.isEmpty()) {
			if(current!=null) {
				stack.push(current);
				current = current.left;
			}
			else {
				current = stack.pop();
				System.out.println(current.val);
				current = current.right;
			}
		}
	}

对比先序遍历和中序遍历,我们可以发现,这两者之间的本质区别在于,先序遍历是入栈之前先访问,而中序遍历是出栈的时候才访问。就代码上的区别来看,不过是把if中push之前的System.out.println(current.val)挪到了else语句块中的pop之后。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,二叉树的遍有三种主要方式:(根-左-右)、(左-根-右)和后(左-右-根)。归的层次遍(也叫广度优,从上到下、从左到右)通常使用队列来辅助实现。 这里分别给出这些遍算法代码: 1. 层(广度优): ```c #include <stdio.h> #include <stdlib.h> #include <queue> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void levelOrder(struct TreeNode* root) { if (root == NULL) return; // 使用队列存储每一层的节点 queue<struct TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { struct TreeNode* node = q.front(); q.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); } printf("\n"); // 换行表示新的一层 } } ``` 2. 归和归两种方式,这里是归版本,使用栈): ```c void preorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; s.push(root); while (!s.empty()) { struct TreeNode* node = s.top(); s.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->right != NULL) s.push(node->right); if (node->left != NULL) s.push(node->left); } } ``` 3. 归,同样使用栈): ```c void inorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; struct TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); printf("%d ", curr->val); // 打印当前节点值 curr = curr->right; } } ``` 4. 后归,使用两个栈): ```c void postorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { struct TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) s1.push(node->left); if (node->right != NULL) s1.push(node->right); } while (!s2.empty()) { struct TreeNode* node = s2.top(); s2.pop(); printf("%d ", node->val); // 打印当前节点值 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值