牛客NC58 找到搜索二叉树中两个错误的节点【中等 非递归中序遍历 提供Java,Go答案】

本文介绍了一种在二叉搜索树中寻找第一个和第二个节点,使得它们的值不满足中序遍历递增顺序的算法,提供了Java和Go两种语言的实现
摘要由CSDN通过智能技术生成

题目

在这里插入图片描述
在这里插入图片描述
题目链接:
https://www.nowcoder.com/practice/4582efa5ffe949cc80c136eeb78795d6?tpId=196&tqId=37154&rp=1&ru=/exam/company&qru=/exam/company&sourceUrl=%2Fexam%2Fcompany&difficulty=undefined&judgeStatus=undefined&tags=&title=

相似题目链接:解法在本题的基础上稍加改造即可
https://www.lintcode.com/problem/3600

前置知识

核心知识:二叉搜索树中序遍历是递增的

参考答案Java

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类 the root
     * @return int整型一维数组
     */
    public int[] findError (TreeNode root) {
        //非递归中序遍历
        TreeNode cur = root;
        //error1:第一个错误节点  error2:第二个错误节点
        TreeNode error1 = null, error2 = null, prev = null;
        Stack<TreeNode> stack = new Stack<>();

        while (!stack.isEmpty() || cur != null) {
            while (cur != null) {
                stack.add(cur);
                cur = cur.left;
            }

            cur = stack.pop();
            if (prev != null) {
                if (prev.val > cur.val && error1 == null) {
                    error1 = prev;
                    error2 = cur;
                } else if (prev.val > cur.val) {
                    error2 = cur;
                }
            }

            prev = cur;
            cur = cur.right;
        }

        int[] ans = {error2.val, error1.val};
        return ans;
    }
}

参考答案Go

package main

import . "nc_tools"

/*
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param root TreeNode类 the root
 * @return int整型一维数组
 */
func findError(root *TreeNode) []int {
	//题目保证节点数大于等于3
	//核心:中序遍历
	var error1, error2, prev TreeNode
	prevInit := false
	error1Init := false
	stack := []TreeNode{} //切片模拟栈

	for len(stack) > 0 || root != nil {
		for root != nil {
			stack = append(stack, *root)
			root = root.Left
		}

		//fmt.Println("stack的长度:", len(stack))
		root = &stack[len(stack)-1]
		stackBak := []TreeNode{}
		for i := 0; i < len(stack)-1; i++ {
			stackBak = append(stackBak, stack[i])
		}
		stack = stackBak
		if prevInit { //prev有值了
			if prev.Val > root.Val && !error1Init {
				error1 = prev
				error2 = *root
				error1Init = true
			} else if prev.Val > root.Val {
				error2 = *root
			}
		}

		prev = *root
		prevInit = true
		root = root.Right
	}

	ans := []int{error2.Val, error1.Val}
	return ans
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵长辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值