Consecutive Parent - Child
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void dfs(TreeNode* A, long long par, int& res) { if(!A) return; res += abs(par - A->val) == 1; dfs(A->left, A->val, res); dfs(A->right, A->val, res); } int Solution::consecutiveNodes(TreeNode* A) { if(!A) return 0; int res = 0; dfs(A,A->val + 2ll,res); return res; }
|