1. 程式人生 > >leetcode700+找出二叉樹中的某值,遞迴

leetcode700+找出二叉樹中的某值,遞迴

https://leetcode.com/problems/search-in-a-binary-search-tree/description/

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    TreeNode* node = NULL;
    void order(TreeNode* root, int value)
    {
        if(root->val==value){
            node = root;
            return;
        }
        if(root->right!=NULL){
            order(root->right, value);
        }
        if(root->left!=NULL){
            order(root->left, value);
        }
    }
    TreeNode* searchBST(TreeNode* root, int val) {
        order(root, val);
        return node;
    }
};