【leetcode】Recover Binary Search Tree

本文介绍了一种在不改变二叉搜索树结构的情况下,通过中序遍历找到并修复被错误交换的两个节点的方法。利用中序遍历的有序特性,能够有效地定位问题节点,并进行值的交换来恢复树的正确状态。

From :https://leetcode.com/problems/recover-binary-search-tree/

Two elements of a binary search tree (BST) are swapped by mistake.

Recover the tree without changing its structure.

Note:
A solution using O( n ) space is pretty straight forward. Could you devise a constant space solution?

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

思路: 二叉树,中序遍历有序。那么任意调换两个节点,必然会让大的结点放在前面,小的结点放在后面。

例如:  中序遍历为1,2,3,4,5,6,7,8,。那么任意调换两个节点,必然大的在前,小的在后。

那么只要找出第一个反序的大的和第二个反序的小的,将两个值交换即可。  但细节上需注意,如果是中序遍历的相邻两个数调换,那么序列中将只有一个逆序对,这时候前一个为大,后一个为小。


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private TreeNode small, large, pre;
    public void recoverTree(TreeNode root) {
        if(null == root) {
            return;
        }
        
        find(root);
        
        if(null != small) {
            swap();
        }
    }
    
    private void find(TreeNode root) {
        if(null == root) {
            return;
        }
        
        find(root.left);
        if(null != pre && pre.val > root.val) {
            small = root;
            if(null == large) {
                large = pre;
            } else {
                return;
            }
        }
        pre = root;
        find(root.right);
    }
    
    private void swap() {
        int t = small.val;
        small.val = large.val;
        large.val = t;
    }
}

下面是美丽的师姐的代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void recoverTree(TreeNode root) {
		TreeNode pre = null;
		TreeNode first = null, second = null;
		// Morris Traversal
		TreeNode temp = null;
		while (root != null) {
			if (root.left != null) {
				// connect threading for root
				temp = root.left;
				while (temp.right != null && temp.right != root)
					temp = temp.right;
				// the threading already exists
				if (temp.right != null) {
					if (pre != null && pre.val > root.val) {
						if (first == null) {
							first = pre;
							second = root;
						} else {
							second = root;
						}
					}
					pre = root;

					temp.right = null;
					root = root.right;
				} else {
					// construct the threading
					temp.right = root;
					root = root.left;
				}
			} else {
				if (pre != null && pre.val > root.val) {
					if (first == null) {
						first = pre;
						second = root;
					} else {
						second = root;
					}
				}
				pre = root;
				root = root.right;
			}
		}
		// swap two node values;
		if (first != null && second != null) {
			int t = first.val;
			first.val = second.val;
			second.val = t;
		}
	}
}


内容概要:本文深入探讨了Django REST Framework(DRF)在毕业设计中的高级应用与性能优化,围绕智能校园系统案例,系统讲解了DRF的核心进阶技术,包括高级序列化器设计、视图集定制、细粒度权限控制、查询优化、缓存策略、异步任务处理以及WebSocket实时通信集成。文章通过详细的代码示例,展示了如何利用DynamicFieldsModelSerializer实现动态字段返回、使用select_related和prefetch_related优化数据库查询、通过Celery实现异步任务、并集成Channels实现WebSocket实时数据推送。同时介绍了基于IP的限流、自定义分页、聚合统计等实用功能,全面提升API性能与安全性。; 适合人群:具备Django和DRF基础,正在进行毕业设计或开发复杂Web API的高校学生及初级开发者,尤其适合希望提升项目技术深度与系统性能的学习者。; 使用场景及目标:①构建高性能、可扩展的RESTful API,应用于智能校园、数据分析、实时监控等毕业设计项目;②掌握DRF高级技巧,如动态序列化、查询优化、缓存、异步任务与实时通信,提升项目竞争力;③优化系统响应速度与用户体验,应对高并发场景。; 阅读建议:此资源以实战为导向,建议读者结合代码逐项实践,重点理解性能优化与架构设计思路,同时动手搭建环境测试缓存、异步任务和WebSocket功能,深入掌握DRF在真实项目中的高级应用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值