513. 找树左下角的值
方法:迭代(层序遍历)
利用队列遍历每一层元素标记每一层第一个元素,这个题用迭代很简单,是个层序遍历的模板题
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
if (root != nullptr) q.push(root);
int ret = 0;
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* front = q.front();
q.pop();
if (i == 0) ret = front->val;
if (front->left) q.push(front->left);
if (front->right) q.push(front->right);
}
}
return ret;
}
};
方法:递归
请找出该二叉树的 最底层 最左边 节点的值
所以也可能是右叶子成为树的最左下角
- 首先,该答案一定出现在深度最大的叶子处
- 又因为要找最左的,所以要先处理左边结点,前中后序都可以,只要保证优先左边搜索,然后记录深度最大的叶子节点
class Solution {
private:
int maxdepth = INT_MIN;
int ret;
public:
void traversal(TreeNode* root, int depth) {
if (root->left == nullptr && root->right == nullptr) {
if (depth > maxdepth) {
maxdepth = depth;
ret = root->val;
}
return;
}
if (root->left) traversal(root->left, depth + 1);
if (root->right) traversal(root->right, depth + 1);
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return ret;
}
};
112. 路径总和
方法:递归
利用递减让计数器count初始为目标和,然后每次减去遍历路径节点上的数值。如果最后count == 0,同时到了叶子节点的话,说明找到了目标和,递归主逻辑中找到合适路径立刻返回
class Solution {
public:
bool traversal(TreeNode* root, int count) {
if (root->left == nullptr && root->right == nullptr && count == 0) return true;
if (root->left == nullptr && root->right == nullptr && count != 0) return false;
if (root->left) {
if (traversal(root->left, count - root->left->val)) return true;
}
if (root->right) {
if (traversal(root->right, count - root->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr)
return false;
return traversal(root, targetSum - root->val);
}
};
将回溯代码显示出来如下
class Solution {
public:
bool traversal(TreeNode* root, int count) {
if (root->left == nullptr && root->right == nullptr && count == 0) return true;
if (root->left == nullptr && root->right == nullptr && count != 0) return false;
if (root->left) {
count -= root->left->val;
if (traversal(root->left, count)) return true;
count += root->left->val;//回溯
}
if (root->right) {
count -= root->right->val;
if (traversal(root->right, count)) return true;
count += root->right->val;//回溯
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr) return false;
return traversal(root,targetSum-root->val);
}
};
精简之后的代码如下
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == nullptr) return false;
if (root->left == nullptr && root->right == nullptr && targetSum == root->val) return true;
return hasPathSum(root->left, targetSum - root->val) ||hasPathSum(root->right, targetSum - root->val);
}
};
113. 路径总和 II
方法:递归
class Solution {
private:
vector<vector<int>> ret;
vector<int> path;
void traversal(TreeNode* root, int count) {
if (!root->left && !root->right && count == 0) {
ret.push_back(path);
return;
}
if (!root->left && !root->right) return;
if (root->left) {
path.push_back(root->left->val);
count -= root->left->val;
traversal(root->left, count);
count += root->left->val;
path.pop_back();
}
if (root->right) {
path.push_back(root->right->val);
count -= root->right->val;
traversal(root->right, count);
count += root->right->val;
path.pop_back();
}
return;
}
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
ret.clear();
path.clear();
if (root == nullptr) return ret;
path.push_back(root->val);
traversal(root, targetSum - root->val);
return ret;
}
};
106. 从中序与后序遍历序列构造二叉树
方法:递归
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return nullptr;
int rootval = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootval);
if (postorder.size() == 1) return root;
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size();delimiterIndex++) {
if (inorder[delimiterIndex] == rootval) break;
}
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end());
postorder.resize(postorder.size() - 1);
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = buildTree(leftInorder, leftPostorder);
root->right = buildTree(rightInorder, rightPostorder);
return root;
}
};
105. 从前序与中序遍历序列构造二叉树
方法:递归
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.size() == 0 || inorder.size() == 0) return nullptr;
int rootval = preorder[0];
TreeNode* root = new TreeNode(rootval);
if (preorder.size() == 1) return root;
int Index;
for (Index = 0; Index < inorder.size(); Index++) {
if (inorder[Index] == rootval) break;
}
vector<int> leftInorder(inorder.begin(), inorder.begin() + Index);
vector<int> rightInorder(inorder.begin() + Index + 1, inorder.end());
vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + 1 + leftInorder.size());
vector<int> rightPreorder(preorder.begin() + 1 + leftInorder.size(), preorder.end());
root->left = buildTree(leftPreorder, leftInorder);
root->right = buildTree(rightPreorder, rightInorder);
return root;
}
};