C++ inorder helper with return boolean flag


  • 0
    H

    Is it good to use a return value indicate when the search should stop?

    bool inOrder(TreeNode* root, int& k, int& ans) {
    	if (!root) return false;
    	if (inOrder(root->left, k, ans)) return true;
    	if (--k==0) { ans=root->val; return true; }
    	if (inOrder(root->right, k, ans)) return true;
    }
    int kthSmallest_mine(TreeNode* root, int k) {
    	int ans;
    	inOrder(root, k, ans);
    	return ans;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.