由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 二叉查找树中两个节点被错误的交换了,如何有效找出他们
相关主题
插入节点到complete binary tree的末尾微软面试的一道题
关于检查Binary tree是否balancedHow to find the kth biggest number in a BST
一个容易记忆的permutation算法BT/BST做题心得
怎么理解递归解决的“swap every two elements in a linked list”?为何找不到很多apple的面筋
如何判断一个tree是另外一个tree的subtree?BST 找重复节点数
这个rebuild binary tree的问题如何计算recursion的空间复杂度
贴一道老算法题请教find number of duplicates in a binary search tree
判断一个树是不是另一个树的子树?EPI 题目
相关话题的讨论汇总
话题: minr话题: maxl话题: root话题: swapped话题: subtree
进入JobHunting版参与讨论
1 (共1页)
r*****e
发帖数: 146
1
二叉查找树中两个节点被错误的交换了,如何有效找出他们。有没有比较neat的解法?
谢谢
l***i
发帖数: 1309
2
assume all nodes are distinct
1. recursively find maxL and minR, which are max element in root->left
subtree and min element in root->right subtree
2. if maxL > minR, then these two are swapped elements
3. if maxL > root value, then maxL is swapped with root
4. if root value > minR, then minR is swapped with root
5. the only case remain is maxL < root < minR, in this case recurse on both
root->left and root->right because the two swapped nodes must be in the same
subtree now.
h****n
发帖数: 1093
3
inorder travese即可找第一个不对路和最后一个不对路的交换即可

二叉查找树中两个节点被错误的交换了,如何有效找出他们。有没有比较neat的解法?
谢谢
★ Sent from iPhone App: iReader Mitbbs Lite 7.56

【在 r*****e 的大作中提到】
: 二叉查找树中两个节点被错误的交换了,如何有效找出他们。有没有比较neat的解法?
: 谢谢

l*********8
发帖数: 4642
4
怎么找maxL 和 minR? 遍历一遍?

both
same

【在 l***i 的大作中提到】
: assume all nodes are distinct
: 1. recursively find maxL and minR, which are max element in root->left
: subtree and min element in root->right subtree
: 2. if maxL > minR, then these two are swapped elements
: 3. if maxL > root value, then maxL is swapped with root
: 4. if root value > minR, then minR is swapped with root
: 5. the only case remain is maxL < root < minR, in this case recurse on both
: root->left and root->right because the two swapped nodes must be in the same
: subtree now.

1 (共1页)
进入JobHunting版参与讨论
相关主题
EPI 题目如何判断一个tree是另外一个tree的subtree?
查找binary tree中有多少个uni-valued subtree这个rebuild binary tree的问题
a question regarding finding all paths with a common sum贴一道老算法题
rebuild a tree from inorder and level order判断一个树是不是另一个树的子树?
插入节点到complete binary tree的末尾微软面试的一道题
关于检查Binary tree是否balancedHow to find the kth biggest number in a BST
一个容易记忆的permutation算法BT/BST做题心得
怎么理解递归解决的“swap every two elements in a linked list”?为何找不到很多apple的面筋
相关话题的讨论汇总
话题: minr话题: maxl话题: root话题: swapped话题: subtree