Leetcode 700. 二叉搜索树中的搜索(DAY 2)

原题题目

Leetcode 700. 二叉搜索树中的搜索(DAY 2)




代码实现

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
#define MAX 5000

struct TreeNode* searchBST(struct TreeNode* root, int val)
{
    if(!root)
        return NULL;
    int top = -1,rear = -1;
    struct TreeNode* queue[MAX];
    struct TreeNode* position;
    queue[++rear] = root;
    while(rear != top)
    {
        position = queue[++top];
        if(position->val == val)
            return position;
        if(position->left)
            queue[++rear] = position->left;
        if(position->right)
            queue[++rear] = position->right;
    }
    return NULL;
}
上一篇:福建南平市获省数字经济发展(区块链)专项补助资金700万元!


下一篇:math模块