Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input: 2 / / 1 3Output:1Example 2:
Input: 1 / / 2 3 / / / 4 5 6 / 7Output:7Note: 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; }};新闻热点
疑难解答