找树左下角的值
其实这道题用层序遍历思路上是最直接的,但用递归法也是可以的,我们需要注意的是必须优先遍历左节点,这样每一次进入更深层时首先遍历到的始终是最左侧节点。因此,前中后序的遍历都行,因为这里没有中间节点的处理,只要保证左在前。
递归
class Solution{
public:
int maxdepth;
int result;
void traversal(TreeNode* node, int depth){
if(!node->left && !node->right){
if(depth > maxdepth){
maxdepth = depth; // 更新更深的深度
result = node->val; // 更新当前深度的最左侧值
}
return;
}
if(node->left) traversal(node->left, depth + 1);
if(node->right) traversal(node->right, depth + 1);
}
int findBottomLeftValue(TreeNode* root){
maxdepth = INT_MIN;
traversal(root, 0);
return result;
}
};
迭代(层序遍历)
class Solution{
public:
int findBottomLeftValue(TreeNode* root){
queue<TreeNode*> que;
que.push(root);
int result;
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;
}
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return result;
}
};
路径总和
这道题主要明确一点,递归函数何时应该返回值。路径总和这道题不需要遍历整棵树,需要返回值来确定是否已经出现满足条件的解。
可以总结如下的规律:
- 如果需要遍历整棵树并且不用处理递归返回值,这时不需要返回值。
- 如果需要遍历整棵树并且需要处理递归返回值,这时需要返回值。
- 如果只需要找到一个符合条件的解,就需要返回值,因为满足条件后要及时返回。
这道题和二叉树路径太像了。一样的写法。
class Solution{
public:
bool traversal(TreeNode* node, int count){
count -= node->val; // 不会出现输入节点为空的情况,所以从剩余和中减去当前的节点元素值
// 叶子节点,同时剩余已经为0,说明找到了一个有效解
if(!node->left && !node->right && count == 0) return true;
if(node->left){ // 如果有左节点,遍历左节点,如果返回true,不用继续寻找解了,直接返回true
if(traversal(node->left, count)) return true;
}
if(node->right){
if(traversal(node->right, count)) return true;
}
return false; // 没有返回true的解,返回false
}
bool hasPathSum(TreeNode* root, int targetSum){
if(!root) return false;
return traversal(root, targetSum);
}
};
迭代法(栈模拟递归)
与遍历所有路径那道题类似,使用两个栈才能实现,一个维护指针,一个维护当前和。
class Solution{
public:
bool hasPathSum(TreeNode* root, int targetSum){
if(!root) return false;
stack<TreeNode*> tree_st;
stack<int> sum_st;
tree_st.push(root);
sum_st.push(root->val);
while(!tree_st.empty()){
TreeNode* node = tree_st.top();
tree_st.pop();
int cur_sum = sum_st.top();
sum_st.pop();
if(!node->left && !node->right && cur_sum == targetSum){
return true;
}
if(node->right){
tree_st.push(node->right);
sum_st.push(cur_sum + node->right->val);
}
if(node->left){
tree_st.push(node->left);
sum_st.push(cur_sum + node->left->val);
}
}
return false;
}
};
从中序与后序遍历序列构造二叉树
关于由遍历序列构造二叉树的问题:可以由后序序列和中序序列唯一确定一个二叉树结构,前序序列和中序序列也可以。原因在于前序和后序都能找到中间节点,但是无法确定左子树和右子树,因此只已知前后序的序列是不能确定唯一二叉树的。
这里描述一下处理过程,将后序遍历的最后一个元素当做中间节点,切割中序序列,再由切割出来的左右子树序列来选择后序序列,这样一层一层递归下去。
class Solution{
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder){
// 如果数组长度为0,返回空指针
if(postorder.size() == 0) return NULL;
// 后序数组有元素,取最后一个元素当做中间节点
int value = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(value); // 新建中间节点
int index = 0; // 寻找中序数组中的分割点
for(; index < inorder.size(); index++){
if(inorder[index] == value) break;
}
// 按照index分割中序数组 [0, index) [index + 1, end)
vector<int> leftInorder(inorder.begin(), inorder.begin() + index);
vector<int> rightInorder(inorder.begin() + index + 1, inorder.end());
// 通过分割后的中序来分割后序
postorder.resize(postorder.size() - 1);
vector<int> leftPostorder(postorder.begin(), postorder.begin() + index);
vector<int> rightPostorder(postorder.begin() + index, postorder.end());
root->left = buildTree(leftInorder, leftPostorder); // 创建左节点
root->right = buildTree(rightInorder, rightPostorder);
return root;
}
};
这种写法需要在递归中不断的定义新数组,既耗时又耗空间。也可以用下标作为递归函数参数,不需要定义新数组。
class Solution{
public:
TreeNode* traversal(vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& postorder, int postorderBegin, int postorderEnd){
// 左闭右开区间
if(postorderBegin == postorderEnd) return NULL;
int value = postorder[postorderEnd - 1];
TreeNode* root = new TreeNode(value);
int index = inorderBegin;
for(;index < inorderEnd; index++){ // index是中序数组中的下标
if(inorder[index] == value) break;
}
// 分割中序数组
int leftInorderBegin = inorderBegin;
int leftInorderEnd = index;
int rightInorderBegin = index + 1;
int rightInorderEnd = inorderEnd;
// 按照中序分割长度来分割后序数组
int leftPostorderBegin = postorderBegin;
int leftPostorderEnd = postorderBegin + index - inorderBegin;
int rightPostorderBegin = postorderBegin + index - inorderBegin;
int rightPostorderEnd = postorderEnd - 1;
root->left = traversal(inorder, leftInorderBegin, leftInorderEnd, postorder, leftPostorderBegin, leftPostorderEnd);
root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder){
return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());
}
};