Advertisement
Guest User

Kth Smallest Element In Tree

a guest
Feb 28th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.27 KB | None | 0 0
  1. int dfs(TreeNode* A, int& B) {
  2.     if (!A) return -1;
  3.     int candidate = dfs(A->left, B);
  4.     if (B == 0) return candidate;
  5.     B--;
  6.     if (B == 0) return A->val;
  7.     return dfs(A->right, B);
  8. }
  9.  
  10. int Solution::kthsmallest(TreeNode* A, int B) {
  11.     return dfs(A, B);
  12. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement