由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Find the node with given value in binary tree in in-order
相关主题
Lowest common ancestor of two nodes of Binary Treecheck if a binary tree is a valid binary search tree
recover binary search tree 常数空间help: leetcode "Recover Binary Search Tree" -- 附代码
F家phone interview的一道题求教Leetcode题目:Lowest Common Ancestor
求教:binary search tree中找第i大的数Find a sub tree with min weight怎么做
Leetcode bst max path-----is this solution correct?讨论几道google题(附个人答案)
问题在哪儿啊 kth Node of BST,大家帮忙Create Binary Tree from preorder and inorder arrays
发现一个很恶心的基础问题回馈本版,新鲜店面,新题新气象
判断 bst 疑问热腾腾的 LinkedIn 电面题攒RP
相关话题的讨论汇总
话题: root话题: find话题: treenode话题: val话题: null
进入JobHunting版参与讨论
1 (共1页)
h****p
发帖数: 87
1
Find the node with given value in binary tree in in-order way,and return it;
PS: the binary tree may include two nodes with the same value.
感觉老写不对
哪位大牛分享下solution?
谢谢
b**********5
发帖数: 7881
2
什么叫 in in-order way, 是不是就写个inorder walk, 然后顺便查查是不是equal?
T******e
发帖数: 157
3
本菜鸟的想法:
inorder遍历的时候如果目标是当前节点,检查左子树(如果有返回值返回)
如果目标不是当前节点,返回左子树(假如有值的话),否则返回右子树
i********s
发帖数: 22
4
不知道理解得是否正确,
如果存在多个值等于val,则返回in-order遍历最先找到的这个节点
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
};
bool find(TreeNode* root, TreeNode*& ret, int val) {
if (root == NULL) {
return false;
}
if (root->left != NULL && find(root->left, ret, val)) {
return true;
}
if (root->val == val) {
ret = root;
return true;
}
if (root->right != NULL && find(root->right, ret, val)) {
return true;
}
return false;
}
s********u
发帖数: 1109
5
首先要确定下是不是BST,这里假定不是。
TreeNode *search(TreeNode *root, int key ){

if( root == NULL )
return NULL;

TreeNode *res = search(root->left,key);

if(res)
return res;

if(root->val == key )
return root;

res = search(root->right,key);
return res;
}
s********u
发帖数: 1109
6
既然root已经处理了null的情况,个人觉得就不用去判断root->left或root->right是
否为NULL了。或者反之。

【在 i********s 的大作中提到】
: 不知道理解得是否正确,
: 如果存在多个值等于val,则返回in-order遍历最先找到的这个节点
: struct TreeNode {
: int val;
: TreeNode* left;
: TreeNode* right;
: };
: bool find(TreeNode* root, TreeNode*& ret, int val) {
: if (root == NULL) {
: return false;

1 (共1页)
进入JobHunting版参与讨论
相关主题
热腾腾的 LinkedIn 电面题攒RPLeetcode bst max path-----is this solution correct?
一道google面试题问题在哪儿啊 kth Node of BST,大家帮忙
狗店面,求BLESS发现一个很恶心的基础问题
recovery BST 不考虑相同值的情况么?判断 bst 疑问
Lowest common ancestor of two nodes of Binary Treecheck if a binary tree is a valid binary search tree
recover binary search tree 常数空间help: leetcode "Recover Binary Search Tree" -- 附代码
F家phone interview的一道题求教Leetcode题目:Lowest Common Ancestor
求教:binary search tree中找第i大的数Find a sub tree with min weight怎么做
相关话题的讨论汇总
话题: root话题: find话题: treenode话题: val话题: null