由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - recover binary search tree 常数空间
相关主题
Find the node with given value in binary tree in in-orderF家phone interview的一道题
Leetcode Recover Binary Search Tree一问求教一道老题
问一道leetcode题:recover BST这个Binary Tree的题来看看
BST 找重复节点数讨论个Binary search tree的题目
一道msft的题判断 bst 疑问
leetcode里面的Recover Binary Search Tree怎么用O(1)spacebinary tree, sum of 2 nodes == given number
树 inorder下个节点最好办法是啥请教find number of duplicates in a binary search tree
Lowest common ancestor of two nodes of Binary TreeF电面
相关话题的讨论汇总
话题: node话题: tree话题: recover话题: prev话题: binary
进入JobHunting版参与讨论
1 (共1页)
m*******1
发帖数: 77
1
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
如果中序遍历后存起来自然是好解决,可是不满足空间要求,如何在O(1)空间做到?
c********t
发帖数: 5706
2
in order traverse不存,只存前一个node,比较current与前一个的值。
如果只有一次不满足 current.val >= previous.val, swap their values.
如果有两次不满足,记录第一次的 previous 和第二次的current, swap their values

【在 m*******1 的大作中提到】
: Two elements of a binary search tree (BST) are swapped by mistake.
: Recover the tree without changing its structure.
: 如果中序遍历后存起来自然是好解决,可是不满足空间要求,如何在O(1)空间做到?

e****e
发帖数: 418
3
Inorder traverse, keep the prev value and prev tree node,
Find first misplaced node by
if ( current.val < prev.val )
Node first = prev;
Find second by
if ( current.val < prev.val )
Node second = current;
After traversal, swap the values of first and second node.
Only need two pointers, prev and current node, and two variables, first and
second node. O(1) space.
1 (共1页)
进入JobHunting版参与讨论
相关主题
F电面一道msft的题
leetcode最难的题目leetcode里面的Recover Binary Search Tree怎么用O(1)space
amazon一道面试题树 inorder下个节点最好办法是啥
bloomberg onsite题Lowest common ancestor of two nodes of Binary Tree
Find the node with given value in binary tree in in-orderF家phone interview的一道题
Leetcode Recover Binary Search Tree一问求教一道老题
问一道leetcode题:recover BST这个Binary Tree的题来看看
BST 找重复节点数讨论个Binary search tree的题目
相关话题的讨论汇总
话题: node话题: tree话题: recover话题: prev话题: binary