Leetcode学习笔记:#700. Search in a Binary Search Tree

Leetcode学习笔记:#700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL.

实现:

public TreeNode searchBST(TreeNode root, int val){
	while(root != null && root.val != val){
		root = val < root.val? root.left :root.right;
	}
	return root;
}

思路:
递归查找二叉树,遇到要找到值停下

猜你喜欢

转载自blog.csdn.net/ccystewart/article/details/90207680
今日推荐