(补卡)代码随想录算法训练营第十四天 | 144/145/94. 二叉树的前/中/后序遍历、二叉树的迭代遍历.

本文介绍了如何通过递归和迭代的方式实现二叉树的前序遍历,包括代码示例和重要注意事项,强调了在编程中的实践与心态调整。

144. 二叉树的前序遍历

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]

文章讲解:代码随想录

视频讲解:每次写递归都要靠直觉? 这次带你学透二叉树的递归遍历!| LeetCode:144.前序遍历,145.后序遍历,94.中序遍历_哔哩哔哩_bilibili

第一印象:不会,二叉树是我的知识盲区,一刷属于是扫盲了。

看完讲解:清晰明了,注意点是写递归的时候思路要清晰,我觉得也是需要反复练习的。

/**
 * 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> result = new ArrayList<>();
        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);
    }
}

//  二叉树的前序迭代遍历
 
class Solution {
    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;
    }
}

//  二叉树的中序递归遍历
 
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorder(root, result);
        return result;
    }
    public void inorder(TreeNode root, List<Integer> result) {
        if (root == null) return;
        inorder(root.left, result);
        result.add(root.val);
        inorder(root.right, result);
    }
}

//  二叉树的中序迭代遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) return result;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty()) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            result.add(node.val);
            node = node.right;
        }
        return result;
    }
}
//  二叉树的后续递归遍历
 
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        postorder(root, result);
        return result;
    }
    public void postorder(TreeNode root, List<Integer> result) {
        if (root == null) return;
        postorder(root.left, result);
        postorder(root.right, result);
        result.add(root.val);
    }
}

//  二叉树的后序迭代遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();    
        if (root == null) return result;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        stack.push(node);
        while (!stack.isEmpty()) {
            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;
    }
}

今日小结:说真的欠的实在是太多了,最近也很忙倒是真的,我希望gap的这段时间能够沉下心来学习,计算机是个很卷的地方,我觉得要摆正心态。

<template> <view class="container"> <view class="search-container"> <view class="search-item name-picker"> <uni-data-picker v-model="item.nickName" placeholder="姓名搜索" @change="handleNameChange" @clear="handleNameClear" clear-icon></uni-data-picker> </view> <view class="search-item date-picker"> <uni-datetime-picker v-model="createTime" type="daterange" @change="handleDateChange" /> </view> </view> <view class="list-item" v-for="item in recordList" :key="item.clockinId"> <view class="item-content"> <view class="left-content"> <text class="nick-name">{{item.nickName}}</text> <text class="time-info">{{getNoteContent(item)}}</text> </view> <view class="right-content" v-if="item.location && item.location !== '定位失败'"> <text class="location">{{item.location}}</text> </view> </view> </view> <view v-if="!loading && recordList.length === 0" class="empty"> 暂无打数据 </view> <view v-if="loading && recordList.length === 0" class="loading"> 加载中... </view> </view> </template> <script> import { clockInList } from '@/api/clockin/clockin.js'; export default { data() { return { recordList: [], loading: false, pageNum: 1, // 当页码 pageSize: 10, // 每页显示条数 total: 0, // 总条数 noData: false } }, onReady() { uni.setNavigationBarTitle({ title: '打记录' }); uni.hideLoading(); }, onShow() { this.pageNum = 1; this.recordList = []; this.fetchCheckinList().catch(error => { console.error('获取打列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); }); }, onPullDownRefresh() { this.initData(); this.fetchCheckinList() .then(() => { uni.stopPullDownRefresh(); // 停止刷新动画 }) .catch(error => { console.error('获取打列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); uni.stopPullDownRefresh(); }); }, onReachBottom() { if (this.loading || this.noData) return; if (this.recordList.length >= this.total && this.total > 0) { this.noData = true; return; } this.pageNum++; this.fetchCheckinList().catch(error => { console.error('获取打列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); }); }, methods: { initData() { this.pageNum = 1; this.recordList = []; this.noData = false; }, // 获取打列表 getstatus(item) { switch (item.type) { case 0: return "上班"; case 1: return "下班"; case 2: return "上班"; default: return "下班"; } }, getNoteContent(item) { return `${this.getstatus(item)} | ${item.createTime}`; }, gettime(item) { return item.createTime; }, fetchCheckinList() { this.loading = true; // 创建干净的请求参数(排除空值) const recordData = { pageNum: this.pageNum, pageSize: this.pageSize, checkin: { nickName: '', location: '', type: '', createTime: '' } }; return clockInList(recordData).then(response => { this.total = response.total || 0; const newData = response.rows || []; if (this.pageNum === 1) { this.recordList = newData; } else { this.recordList = [...this.recordList, ...newData]; } // 检查是否已加载全部数据 if (this.recordList.length >= this.total) { this.noData = true; } }).catch(error => { console.error('获取审批列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); }).finally(() => { this.loading = false; }); }, // handleDateChange() { // // 正确设置时间范围参数 // if (this.createTime && this.createTime.length === 2) { // this.queryParams.beginTime = this.createTime[0]; // this.queryParams.endTime = this.createTime[1]; // } else { // // 清空无效的时间范围 // this.queryParams.beginTime = undefined; // this.queryParams.endTime = undefined; // } // this.queryParams.pageNum = 1 // this.fetchCheckinList() // }, } } </script> <style> .container { padding: 0; } .search-container { padding: 15rpx; background-color: #fff; display: flex; flex-wrap: nowrap; /* 禁止换行 */ gap: 10rpx; overflow-x: auto; } /* 姓名搜索框 */ .search-item.name-picker { flex: 1.2; min-width: 200rpx; } .search-item.date-picker { flex: 2; min-width: 300rpx; } .picker { padding: 16rpx 20rpx; border: 1rpx solid #e5e5e5; border-radius: 8rpx; font-size: 14px; text-align: center; white-space: nowrap; } .watermark { color: #c0c0c0 !important; opacity: 0.8; } .list-item { border-radius: 12rpx; background-color: #fafafa; border-bottom: 1px solid #eee; padding: 10px; margin-bottom: 10px; } .item-content { border-radius: 12rpx; } .item-content text { margin-bottom: 5px; font-size: 14px; } .left-content { flex-shrink: 0; display: flex; flex-direction: column; margin-right: 20rpx; white-space: nowrap; /* 确保左侧内容不换行 */ } .right-content { flex-grow: 1; text-align: right; word-break: break-all; /* 允许地址换行 */ } .nick-name { font-size: 16px; color: #333; margin-bottom: 5px; } .time-info { font-size: 14px; color: #666; } .location { font-size: 12px; color: #999; } .loading, .empty { text-align: center; padding: 20rpx; color: #999; font-size: 14px; } </style> chunk-vendors.js:20834 [Vue warn]: Error in render: "TypeError: Cannot read properties of undefined (reading 'nickName')"
07-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值