题目链接:https://leetcode-cn.com/problems/search-in-a-binary-search-tree/
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。
例如,
给定二叉搜索树:
4
/ \
2 7
/ \
1 3
和值: 2
你应该返回如下子树:
2
/ \
1 3
在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。
#################################################################
先上答案,递归和迭代两种方法,官方答案如下:
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || val == root.val) return root;
return val < root.val ? searchBST(root.left, val) : searchBST(root.right, val);
}
}
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null && val != root.val)
root = val < root.val ? root.left : root.right;
return root;
}
}
我的写法,丑陋的代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode r;
public TreeNode searchBST(TreeNode root, int val) {
qx(root,val);
if(r==null){
return null;
}else{
return r;
}
}
public void qx(TreeNode root, int val){
if(root.left!=null)qx(root.left,val);
if(root.val==val)r=root;
if(root.right!=null)qx(root.right,val);
}
}