Fork me on GitHub

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);
    }
};

 

posted @ 2018-12-04 20:33  lMonster81  阅读(84)  评论(0编辑  收藏  举报
/*评论*/ /*top按钮*/

/* 网易云控件 */