由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - help: leetcode "Recover Binary Search Tree" -- 附代码
相关主题
Recover Binary Search Tree:以前的解法通不过了MS onsite面经
问一个题目面试的时候 binary tree的delete也要15分钟之内写完么?
发现一个很恶心的基础问题有没有人同觉得Recover Binary Search Tree的solution using O(n) space并不是那么straight forward么?
Leetcode bst max path-----is this solution correct?请问LOWEST COMMON ANCESTOR OF A BINARY TREE, treenode 只有parent,没有left,right
[leetcode] Maximum Depth of Binary Tree弱问:leetcode里Convert Sorted List to Binary Search Tree
check if a binary tree is a valid binary search treeleetcode Runtime error : Flatten Binary Tree to Linked List
Find the node with given value in binary tree in in-order为啥有两个case不对??Binary Tree Maximum Path Sum
leetcode交了钱的能share一下题么?Binary Tree Maximum Path Sum
相关话题的讨论汇总
话题: treenode话题: null话题: root话题: binary话题: recover
进入JobHunting版参与讨论
1 (共1页)
k*o
发帖数: 2
1
求牛人帮忙找bug。
题目是leetcode上的"Recover Binary Search Tree"。
思路是中序遍历,用递归做。
总是过不了OJ,在自己电脑上可以跑。
求大牛帮忙看看怎么回事。跪谢了!!!
我的代码如下:
public class Solution {
TreeNode node1 = null;
TreeNode node2 = null;
TreeNode last = null;
TreeNode current = null;
public void dfs(TreeNode root) {
if(root == null) {
return;
}
TreeNode left = root.left;
TreeNode right = root.right;
dfs(left);
last = current;
current = root;
if(last!=null && last.val>current.val) {
if(node1 == null) {
node1 = last;
node2 = current;
} else {
node2 = current;
}
}
dfs(right);
}
public void recoverTree(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null) {
return;
}
dfs(root);
int temp = node1.val;
node1.val = node2.val;
node2.val = temp;
}
}
b*****e
发帖数: 131
2
class members 要重新初始化
k*o
发帖数: 2
3

AC了。
多谢 mbwayne

【在 b*****e 的大作中提到】
: class members 要重新初始化
1 (共1页)
进入JobHunting版参与讨论
相关主题
Binary Tree Maximum Path Sum[leetcode] Maximum Depth of Binary Tree
讨论一道LeetCode题:Binary Tree Maximum Path Sumcheck if a binary tree is a valid binary search tree
问一个leetcode上面binary tree的题目Find the node with given value in binary tree in in-order
判断是不是binary search tree-leetcodeleetcode交了钱的能share一下题么?
Recover Binary Search Tree:以前的解法通不过了MS onsite面经
问一个题目面试的时候 binary tree的delete也要15分钟之内写完么?
发现一个很恶心的基础问题有没有人同觉得Recover Binary Search Tree的solution using O(n) space并不是那么straight forward么?
Leetcode bst max path-----is this solution correct?请问LOWEST COMMON ANCESTOR OF A BINARY TREE, treenode 只有parent,没有left,right
相关话题的讨论汇总
话题: treenode话题: null话题: root话题: binary话题: recover