Leetcode513. Find Bottom Left Tree Value找树左下角的值

给定一个二叉树,在树的最后一行找到最左边的值。

示例 1:

输入: 2 / 1 3 输出: 1

示例 2:

输入: 1 / 2 3 / / 4 5 6 / 7 输出: 7

注意: 您可以假设树(即给定的根节点)不为 NULL。

class Solution {
public:
    int maxDepth;
    int res;
    int findBottomLeftValue(TreeNode* root) 
    {
        maxDepth = 0;
        GetAns(1, root);
        return res;
    }

    void GetAns(int depth, TreeNode* root)
    {
        if(root == NULL)
            return;
        if(depth > maxDepth)
        {
            maxDepth = depth;
            res = root ->val;
        }
        if(root ->left)
            GetAns(depth + 1, root ->left);
        if(root ->right)
            GetAns(depth + 1, root ->right);
    }
};
原文地址:https://www.cnblogs.com/lMonster81/p/10433810.html