【leetcode刷题笔记】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?



题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来。

排序二叉树的中序遍历得到的一定是一个递增序列,例如上述所示的BST,中序遍历得到的序列为1,3,5,9,11,15,20。假设错位的是3和11,那么错位后的树如下图所示

用遍历firstNode和secondNode表示错位的两个点,那么在中序遍历过程中,第一个错位点后面的点一定比它小(5比11小,11才是错位的点);第二个错位点一定比它前面的点小(3比9小)。采用递归的方法中序遍历BST,如果当前遍历的点root比它前面的点prev的值小,有两种可能,第一种是prev是第一个错位的点,此时firstNode变量为空,则将firstNode赋值为prev(代码中第24行);第二种是root是第二个错位的点,此时firstNode变量部位空,将secondNode变量赋值为root(代码中第22行)。

代码如下:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     private TreeNode firstNode = null;
12     private TreeNode secondNode = null;
13     private TreeNode prev = null;
14
15     private void traverse(TreeNode root){
16         if(root == null)
17             return;
18
19         traverse(root.left);
20
21         if(prev != null && root.val < prev.val){
22             secondNode = root;
23             if(firstNode == null)
24                 firstNode = prev;
25         }
26         prev = root;
27         traverse(root.right);
28
29     }
30
31     public void recoverTree(TreeNode root) {
32         traverse(root);
33
34         int temp = firstNode.val;
35         firstNode.val = secondNode.val;
36         secondNode.val = temp;
37     }
38 }

【leetcode刷题笔记】Recover Binary Search Tree

时间: 2024-10-31 18:01:16

【leetcode刷题笔记】Recover Binary Search Tree的相关文章

【leetcode刷题笔记】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

刷题感悟 - Convert Binary Search Tree to Doubly Linked List

将二叉查找树转化成双向链表 题目思路其实不难 ,中序遍历,然后再依次的将数据放入链表中即可 重点:新加元素前后链的配置 有个有意思的地方在于result没有赋初值 导致写代码时需要先初始化 然后删除之 . /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val =

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样. 自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=.= 今天早上终于弄懂了,用递归

【leetcode刷题笔记】Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解:递归的枚举1~n的每个节点为根节点,然后递归

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

[leetcode]Recover Binary Search Tree @ Python

原题地址:https://oj.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. 解题思路:这题是说一颗二叉查找树中的某两个节点被错误的交换了,需要恢复成原来的正确的二叉查找树. 算法一:思路很简单,一颗二叉查