代码随想录第1天 | 二叉树理论基础 递归遍历 迭代遍历


二叉树的递归遍历

递归三要素
1、确定递归函数的参数和返回值:确定哪些参数是递归的过程中需要处理的,就在递归函数中加上这个参数,并且还要明确每次递归的返回值是什么,进而确定递归函数的返回类型。
2、确定终止条件:写完了递归算法,运行的时候会爆栈,因为没有写终止条件或者是终止条件不对,操作系统使用栈来保存每层递归,递归没有终止,操作系统的内存栈就会溢出。
3、确定单层递归的逻辑:确定每一层递归需要处理的信息。

例如:

  • 二叉树的前序遍历
    1.确定递归函数的参数和返回值:传入参数为根节点,存储结果的List集合
void preorder(TreeNode root, List<Integer> result)

2.确定终止条件:

if (cur == null) {
	return;
}

3.确定单层递归的逻辑

result.add(root.val);
preorder(root.left, result);
preorder(root.right, result);

144.二叉树的前序遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        preorder(root, result);
        return result;
    }

    public void preorder(TreeNode root, List<Integer> result) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        preorder(root.left, result);
        preorder(root.right, result);
    }
}

二叉树的迭代遍历

  • 前序遍历:中 - 左 - 右,入栈顺序:中 - 右 - 左
public List<Integer> preorderTraversal(TreeNode root){
	List<Integer> result = new ArrayList<>();
	if (root == null) {
		return result;
	}
	Stack<TreeNode> stack = new Stack<>();
	stack.push(root);
	while (!stack.isEmpty) {
		TreeNode node = stack.pop();
		result.add(node.val);
		if (node.right != null) {
			stack.push(node.right);
		}
		if (node.left != null) {
			stack.push(node.left);
		}
	}
	return result;
}
  • 中序遍历:左 - 中 - 右, 入栈顺序:左 - 右
public List<Integer> inorderTraversal(TreeNode root) {
	List<Integer> result = new ArrayList<>();
	if (root == null) {
		return result;
	}
	Stack<TreeNode> stack = new Stack<>();
	TreeNode cur = root;
	while (cur != null || !stack.isEmpty()) {
		if (cur != null) {
			stack.push(cur);
			cur = cur.left;
		}else{
			cur = stack.pop();
			result.add(cur.val);
			cur = cur.right;
		}
	}
	return result;
}
  • 后序遍历:左 - 右 - 中,入栈顺序:中 - 左 - 右, 出栈顺序:中 - 右 - 左
public List<Integer> postorderTraversal(TreeNode root) {
	List<Integer> result = new ArrayList<>();
	if (root == null) {
		return result;
	}
	Stack<TreeNode> stack = new Stack<>();
	stack.push(root);
	while (!stack.isEmpty()) {
		TreeNode node = stack.pop();
		result.add(node.val);		
		if (node.left != null) {
			stack.push(node.left);			
		}
		if (node.right != null) {
			stack.push(node.right);			
		}
	}
	Collections.reverse(result);
	return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二叉树遍历有三种方式:前序遍历、中序遍历和后序遍历。其中,递归遍历是比较常见的方式,而非递归遍历则需要借助栈的数据结构实现。 下面给出二叉树递归遍历和非递归遍历的C语言代码实现: 1. 二叉树递归遍历 前序遍历: ``` void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->val); // 访问根节点 preorderTraversal(root->left); // 递归遍历左子树 preorderTraversal(root->right); // 递归遍历右子树 } ``` 中序遍历: ``` void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); // 递归遍历左子树 printf("%d ", root->val); // 访问根节点 inorderTraversal(root->right); // 递归遍历右子树 } ``` 后序遍历: ``` void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); // 递归遍历左子树 postorderTraversal(root->right); // 递归遍历右子树 printf("%d ", root->val); // 访问根节点 } ``` 2. 二叉树的非递归遍历 前序遍历: ``` void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } stack<TreeNode*> st; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); printf("%d ", node->val); // 访问节点 if (node->right != NULL) { st.push(node->right); // 右子节点先入栈,保证左子节点先出栈 } if (node->left != NULL) { st.push(node->left); } } } ``` 中序遍历: ``` void inorderTraversal(TreeNode* root) { stack<TreeNode*> st; TreeNode* node = root; while (!st.empty() || node != NULL) { if (node != NULL) { // 当前节点不为空,继续将其左子节点入栈 st.push(node); node = node->left; } else { // 当前节点为空,说明已经到达最左侧,开始出栈访问节点 node = st.top(); st.pop(); printf("%d ", node->val); // 访问节点 node = node->right; // 开始访问右子节点 } } } ``` 后序遍历: ``` void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } stack<TreeNode*> st1, st2; st1.push(root); while (!st1.empty()) { TreeNode* node = st1.top(); st1.pop(); st2.push(node); // 先将当前节点入栈st2 if (node->left != NULL) { st1.push(node->left); // 左子节点入栈st1 } if (node->right != NULL) { st1.push(node->right); // 右子节点入栈st1 } } while (!st2.empty()) { // 出栈访问节点 TreeNode* node = st2.top(); st2.pop(); printf("%d ", node->val); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值