700. Search in a Binary Search Tree
Given the tree:
4
/ \
2 7
/ \
1 3
And the value to search: 2 2
/ \
1 3解题要点:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while(root != null){
if(val < root.val) root = root.left;
else if(val > root.val) root = root.right;
else return root;
}
return null;
}
}Last updated