struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x), left = nullptr, right = nullptr{}
};
class solution(){
public:
TreeNode solution(TreeNode *root){
dfs(root).second;
}
pair<int, TreeNode*> dfs(TreeNode *Node){
//深度遍历二叉树
if(!Node) return make_pair(0, nullptr);
auto [size_l, node_l] = dfs(Node->left);
auto [size_r, node_r] = dfs(Node->right);
if(size_l == size_r){
return [size_l + 1, Node];
}
if(size_l > size_r){
return [size_l + 1, node_l];
}
return [size_r + 1, node_r];
}
};
熟能生巧之pair
玩的越来越花了