给定一个二叉树,在树的最后一行找到最左边的值。
示例 1:
输入:
2
/ \
1 3
输出:
1
示例 2:
输入:
1
/ \
2 3
/ / \
4 5 6
/
7
输出:
7
思路:pair <层数,节点值> 先序遍历树,或者说DFS
执行用时 : 32 ms, 在Find Bottom Left Tree Value的C++提交中击败了93.40% 的用户 内存消耗 : 21.4 MB, 在Find Bottom Left Tree Value的C++提交中击败了20.94% 的用户
class Solution {
pair<int, int> maxPair;
public:
int findBottomLeftValue(TreeNode* root) {
vector<pair<int, int>> vp;
dfs(root, vp, 0);
int max = -1;
for (auto p : vp) {
if (p.first>max) {
maxPair = p;
max = p.first;
}
}
return maxPair.second;
}
void dfs(TreeNode* root, vector<pair<int, int>>& vp, int depth) {
if (root == NULL) {
// vp.push_back(make_pair(depth,0));
return;
}
vp.push_back(make_pair(depth, root->val));
dfs(root->left, vp, depth + 1);
dfs(root->right, vp, depth + 1);
}
};
优化:层次遍历
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode *> nodes;
TreeNode *node;
int size;
nodes.push(root);
while (!nodes.empty()) {
size = nodes.size();
node = nodes.front();
while (size-- > 0) {
root = nodes.front();
nodes.pop();
if (root->left) nodes.push(root->left);
if (root->right) nodes.push(root->right);
}
}
return node->val;
}
};
完整测试代码:
#include "stdafx.h"
#include <sstream>
#include <queue>
// 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 {
pair<int, int> maxPair;
public:
int findBottomLeftValue(TreeNode* root) {
vector<pair<int, int>> vp;
dfs(root, vp, 0);
int max = -1;
for (auto p : vp) {
if (p.first>max) {
maxPair = p;
max = p.first;
}
}
return maxPair.second;
}
void dfs(TreeNode* root, vector<pair<int, int>>& vp, int depth) {
if (root == NULL) {
// vp.push_back(make_pair(depth,0));
return;
}
vp.push_back(make_pair(depth, root->val));
dfs(root->left, vp, depth + 1);
dfs(root->right, vp, depth + 1);
}
};
void trimLeftTrailingSpaces(string &input) {
input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
return !isspace(ch);
}));
}
void trimRightTrailingSpaces(string &input) {
input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
return !isspace(ch);
}).base(), input.end());
}
TreeNode* stringToTreeNode(string input) {
trimLeftTrailingSpaces(input);
trimRightTrailingSpaces(input);
input = input.substr(1, input.length() - 2);
if (!input.size()) {
return nullptr;
}
string item;
stringstream ss;
ss.str(input);
getline(ss, item, ',');
TreeNode* root = new TreeNode(stoi(item));
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (true) {
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
if (!getline(ss, item, ',')) {
break;
}
trimLeftTrailingSpaces(item);
if (item != "null") {
int leftNumber = stoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (!getline(ss, item, ',')) {
break;
}
trimLeftTrailingSpaces(item);
if (item != "null") {
int rightNumber = stoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
void leet513Test() {
string line;
while (getline(cin, line)) {
TreeNode* root = stringToTreeNode(line);
int ret = Solution().findBottomLeftValue(root);
string out = to_string(ret);
cout << out << endl;
}
}
测试案例:
[2,1,3]
输出
1
预期结果
1