题解 | #找到搜索二叉树中两个错误的节点#

找到搜索二叉树中两个错误的节点

https://www.nowcoder.com/practice/4582efa5ffe949cc80c136eeb78795d6

/*
 * function TreeNode(x) {
 *   this.val = x;
 *   this.left = null;
 *   this.right = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param root TreeNode类 the root
 * @return int整型一维数组
 */
function findError( root ) {
    // write code here
    let first = null, second = null, prev = null;

  function inorder(node) {
    if (!node) return;

    inorder(node.left);

    if (prev && prev.val > node.val) {
      if (!first) {
        first = prev;
      }
      second = node;
    }
    prev = node;

    inorder(node.right);
  }

  inorder(root);

  return [first.val, second.val].sort((a, b) => a - b);
}
module.exports = {
    findError : findError
};

全部评论

相关推荐

许愿ssp的咸鱼很不想泡池子:import python as pyhton
点赞 评论 收藏
分享
什么时候才能有offer啊_:十年前我还在刺激战场研究跳伞的底层原理呢
投递牛客等公司
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务