二叉樹(shù)|513.找樹(shù)左下角的值幔崖、112. 路徑總和食店、106.構(gòu)造二叉樹(shù)
513.找樹(shù)左下角的值
自己審題思路
層序遍歷找最后一層第一個(gè)元素
看完代碼隨想錄題解后的收獲
遞歸法的學(xué)習(xí)
代碼:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int result = 0;
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (i == 0) result = node->val; // 記錄每一行第一個(gè)元素(循環(huán)覆蓋,最終記錄最后一行第一個(gè)元素)
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};
代碼(遞歸)
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* cur, int depth) {
if(!cur->left && !cur->right) {
if(depth > maxDepth) {
maxDepth = depth; // 更新最大深度
result = cur->val; // 最大深度最左面的數(shù)值
}
return;
}
if(cur->left) traversal(cur->left, depth + 1);
if(cur->right) traversal(cur->right, depth + 1);
return;
}
int findBottomLeftValue(TreeNode* root) {
if(root == nullptr) return 0;
traversal(root, 0);
return result;
}
};
參考詳解
112. 路徑總和
自己審題思路
看完代碼隨想錄題解后的收獲
遞歸函數(shù)什么時(shí)候要有返回值赏寇,什么時(shí)候沒(méi)有返回值(三點(diǎn))
- 如果需要搜索整棵二叉樹(shù)且不用處理遞歸返回值吉嫩,遞歸函數(shù)就不要返回值。(113.路徑總和ii)
- 如果需要搜索整棵二叉樹(shù)且需要處理遞歸返回值,遞歸函數(shù)就需要返回值。 (236. 二叉樹(shù)的最近公共祖先 )
- 如果要搜索其中一條符合條件的路徑币呵,那么遞歸一定需要返回值平匈,因?yàn)橛龅椒蠗l件的路徑了就要及時(shí)返回。(本題的情況)
代碼:
class Solution {
public:
bool traversal(TreeNode* cur, int count) {
if(!cur->left && !cur->right && count == 0) return true;
if(!cur->left && !cur->right) return false;
if (cur->left) {
if(traversal(cur->left,count - cur->left->val)) return true;
}
if (cur->right) {
if(traversal(cur->right,count - cur->right->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == nullptr) return false;
return traversal(root, targetSum - root->val);
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool traversal(TreeNode* cur, int count) {
if(!cur->left && !cur->right && count == cur->val) return true;
if(!cur->left && !cur->right) return false;
if (cur->left) {
if(traversal(cur->left,count - cur->val)) return true;
}
if (cur->right) {
if(traversal(cur->right,count - cur->val)) return true;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == nullptr) return false;
return traversal(root, targetSum);
}
};
參考詳解
構(gòu)造二叉樹(shù)
自己審題思路
這道題有思路:根據(jù)中序找到根節(jié)點(diǎn)窗骑,然后循環(huán)找,但是寫(xiě)代碼的時(shí)候遇到不少問(wèn)題。
看完代碼隨想錄題解后的收獲
理解思路和debug动漾。
代碼:
class Solution {
private:
TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
if (postorder.size() == 0) return NULL;
// 后序遍歷數(shù)組最后一個(gè)元素,就是當(dāng)前的中間節(jié)點(diǎn)
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
// 葉子節(jié)點(diǎn)
if (postorder.size() == 1) return root;
// 找到中序遍歷的切割點(diǎn)
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序數(shù)組
// 左閉右開(kāi)區(qū)間:[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
// postorder 舍棄末尾元素
postorder.resize(postorder.size() - 1);
// 切割后序數(shù)組
// 依然左閉右開(kāi)荠锭,注意這里使用了左中序數(shù)組大小作為切割點(diǎn)
// [0, leftInorder.size)
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
return root;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder, postorder);
}
};
class Solution {
public:
TreeNode* dfs(vector<int>& inorder, int inStart, int inEnd, vector<int>& postorder, int postStart, int postEnd) {
if(inStart == inEnd ) return nullptr;
int rootValue = postorder[postEnd - 1];
int split;
for (int i = 0; i < inorder.size(); i++) {
if (rootValue == inorder[i]) {
split = i;
break;
}
}
TreeNode* root = new TreeNode(rootValue);
int inLeftStart = inStart;
int inLeftEnd = split;
int inRightStart = split + 1;
int inRightEnd = inEnd;
int postLeftStart = postStart;
int postLeftEnd = postLeftStart + (inLeftEnd - inLeftStart);
int postRightStart = postLeftEnd;
int postRightEnd = postEnd - 1;
root->left = dfs(inorder, inLeftStart, inLeftEnd, postorder, postLeftStart, postLeftEnd);
root->right = dfs(inorder, inRightStart, inRightEnd, postorder, postRightStart, postRightEnd);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0 || postorder.size() == 0) return nullptr;
return dfs(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};