题干:
用层序遍历方便些,因为只需要把res不断替换成每一层第一个节点值即可,代码如下:
class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*>que;if(root != NULL)que.push(root);int res = 0;while(!que.empty()){int size = que.size();for(int i = 0; i < size; i++){TreeNode* node = que.front();que.pop();if(i == 0) res = node -> val;if(node -> left)que.push(node -> left);if(node -> right)que.push(node -> right);}}return res;}
};