Last Node in a Complete Binary Tree
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, int dep, pair<int,int>& res) { if(!A) return; if(res.first < dep) res = {dep,A->val}; else if(res.first == dep) res.second = A->val; dfs(A->left, dep + 1, res); dfs(A->right, dep + 1, res); } int Solution::lastNode(TreeNode* A) { pair<int, int> res{-1,-1}; dfs(A, 0, res); return res.second; }
|