LeetCode 513. Find Bottom Left Tree Value(找到树中左下角的节点)

题意:找到树中左下角的节点。

分析:层次遍历,每层从右往左遍历,最后一个结点即为左下角结点。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            root = q.front();
            q.pop();
            if(root -> right != NULL) q.push(root -> right);
            if(root -> left != NULL) q.push(root -> left);
        }
        return root -> val;
    }
};
原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/12531575.html