首页 > 学院 > 开发设计 > 正文

513. Find Bottom Left Tree Value

2019-11-08 02:00:28
字体:
来源:转载
供稿:网友

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

Input: 2 / / 1 3Output:1

Example 2:

Input: 1 / / 2 3 / / / 4 5 6 / 7Output:7

Note: You may assume the tree (i.e., the given root node) is not NULL.

/** * 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 maxLevel = 0; int val; void dfs(TreeNode* root, int level){ if(root == NULL) return ; if(level > maxLevel){ maxLevel = level; val = root->val; } dfs(root->left, level + 1); dfs(root->right, level + 1); } int findBottomLeftValue(TreeNode* root) { val = root->val; dfs(root, 1); return val; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表