Search in Binary tree
2007-09-19 09:05
162 查看
/*
Given a binary tree, return true if a node
with the target data is found in the tree. Recurs
down the tree, chooses the left or right
branch by comparing the target to each node.
*/
bool int lookup(struct node* node, int target) {
// 1. Base case == empty tree
// in that case, the target is not found so return false
if (node == NULL) {
return(false);
}
else {
// 2. see if found here
if (target == node->data) return(true);
else {
// 3. otherwise recur down the correct subtree, this is hightlight.
if (target < node->data) return(lookup(node->left, target));
else return(lookup(node->right, target));
}
}
}
Given a binary tree, return true if a node
with the target data is found in the tree. Recurs
down the tree, chooses the left or right
branch by comparing the target to each node.
*/
bool int lookup(struct node* node, int target) {
// 1. Base case == empty tree
// in that case, the target is not found so return false
if (node == NULL) {
return(false);
}
else {
// 2. see if found here
if (target == node->data) return(true);
else {
// 3. otherwise recur down the correct subtree, this is hightlight.
if (target < node->data) return(lookup(node->left, target));
else return(lookup(node->right, target));
}
}
}
相关文章推荐
- Leetcode 501 Find Mode in Binary Search Tree
- Leetcode 255 Verify Preorder Sequence in Binary Search Tree
- Insert Node in a Binary Search Tree
- [study] BinarySearchTree class in Java
- 501. Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- Find the nearest common ancestor of any two nodes in a binary search tree
- Find Mode in Binary Search Tree
- [Leetcode]Verify Preorder Sequence in Binary Search Tree
- Find Mode in Binary Search Tree
- 04-树4. Search in a Binary Search Tree (25)
- Remove Node in Binary Search Tree
- [刷题]Search Range in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- LeetCode Find Mode in Binary Search Tree
- Find mode in Binary Search Tree
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- binary search tree in C++
- 04-树7. Search in a Binary Search Tree (25)