Day i do not know
This commit is contained in:
22
700. Search in a Binary Search Tree/Solution.cs
Normal file
22
700. Search in a Binary Search Tree/Solution.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace _700._Search_in_a_Binary_Search_Tree;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public TreeNode? SearchBST(TreeNode? root, int val)
|
||||
{
|
||||
Stack<TreeNode> stack = new Stack<TreeNode>();
|
||||
if (root != null)
|
||||
stack.Push(root);
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
TreeNode cur = stack.Pop();
|
||||
if (cur.right != null)
|
||||
stack.Push(cur.right);
|
||||
if (cur.left != null)
|
||||
stack.Push(cur.left);
|
||||
if(cur.val == val)
|
||||
return cur;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user