寻找二叉树最左边的叶子节点

题目理解

最左边的叶子节点,就是最后一层最左边的节点,该节点也可以是右节点。

思路

找到深度最大的叶子节点。

层序的思路简单,但是这里先用递归试试。

1.参数:节点;返回值:int;不对,求的是叶子节点,所以返回的是叶子节点的val;深度用int记录在参数里面,用maxDepth记录最大深度;result记录最大深度最大点的值。

void tranverse(TreeNode* node,int depth)
int maxDepth=INT_MAX;
int result;

2.终止条件:找到最大深度的叶子节点。

if(node->left==NULL&node->right==NULL){return ;{
if(depth>maxDepth){
maxDepth=depth;
result=node->val;
}
}

3.单层递归逻辑

depth++;
tranverse(node->left,depth);
depth--;
}
return;
if(node->right){
depth++;
tranverse(node->right);
depth--;
}
return;

完整代码:

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        int depth;
        tranverse(root,depth);
        return result;
    }
    int maxDepth=INT_MIN;
    int result;
    void tranverse(TreeNode* node,int depth){
    if(node->left==NULL&&node->right==NULL){
    if(depth>maxDepth)
    {
        maxDepth=depth;
        result=node->val;
    }    
    }
    if(node->left){
        depth++;
        tranverse(node->left,depth);
        depth--;
    }
    if(node->right){
        depth++;
        tranverse(node->right,depth);
        depth--;
    }
    return;
    }
};

层序遍历写法

public:
    int findBottomLeftValue(TreeNode* root) {
        vector<int> vec = levelOrder(root);
        return vec[0];
    }
    vector<int> levelOrder(TreeNode* root) {
        queue<TreeNode*> que;
        vector<int> vect;
        if (root != NULL)
            que.push(root);
        while (!que.empty()) {
            int size = que.size();
            vect.clear();
            while (size--) {
                TreeNode* node = que.front();
                que.pop();
                vect.push_back(node->val);
                if (node->left)
                    que.push(node->left);
                if (node->right)
                    que.push(node->right);
            }
        }
        return vect;
    }

路径总和

题目理解

给一个值,判断是否存在一条路径,该路径上根节点到叶子结点的和为该值。

思路

做过数组内两数和,三数和,四数和,思路都是固定一个,找另一个。不过这次数据结构变了。如果是在二叉树中,元素个数不确定。应该有回溯的一部。
找叶子值为和与之前经过路径上节点值之和。

1.参数:节点;返回值为bool类型。
2.终止条件:到达叶子节点count==0,true;到达叶子节点,count!=0,false.
3.单层循环逻辑:
遇到左节点,count差,然后递归返回true,复原count;

代码

public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL)  return false;
        int count=targetSum-root->val;
        return tranverse(root,count);
    }
    bool tranverse(TreeNode* node,int count){
        if(node->left==NULL&&node->right==NULL&&count==0)
        return true;
        if(node->left==0&&node->right==0&&count<0)
        return false;
        if(node->left){
            count-=node->left->val;
            if(tranverse(node->left,count))
            return true;
            count+=node->left->val;
        }
        if(node->right){
            count-=node->right->val;
            if(tranverse(node->right,count))
            return true;
            count+=node->right->val;
        }
        return false;
    }
    
};

根据后序和中序求前序

思路:

1.返回值是一个数组,vector<int>;参数是传入的两个序列:后序序列和中序序列,都是vector<int>
2.终止条件
如果数组大小为0,返回NULL;
如果只有一个节点,返回root。
3.单层递归逻辑

  • 在后序找到根节点;
  • 在中序找到根节点对应的位置索引;
  • 根据索引位置分割中序序列左、中序序列右;
  • 根据中序序列左序列大小作为分割点分割后序序列;
  • 遍历左子树的左中序序列和左后序序列;
  • 遍历右子树的左中序序列和左后序序列;
  • 返回root;

1.在分割后序数组的时候用左中序数组的大小作为切割点,没想到。
2.区间点的开闭;

代码

public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return traversal(inorder,postorder);
    }
    TreeNode * traversal(vector<int>& inorder ,vector<int>postorder){
        if(postorder.size()==0) return NULL;
        int rootValue=postorder[postorder.size()-1];
        TreeNode *root = new TreeNode(rootValue);
        if(postorder.size()-1==0) return root;
        int index;
        for(index=0;index<inorder.size();index++){
        if (inorder[index]==rootValue)
        break;
    }
    //切割中序数组
    vector<int> inorderLeft (inorder.begin(),inorder.begin()+index);
    vector<int> inorderRight (inorder.begin()+index+1,inorder.end());
    //切割后序数组
    vector<int> postorderL (postorder.begin(),postorder.begin()+inorderLeft.size());
    vector<int> postorderR (postorder.begin()+inorderLeft.size(),postorder.end()-1);
    //
    root->left=traversal(inorderLeft,postorderL);
    root->right=traversal(inorderRight,postorderR);
    return root;
    }
};```
posted on 2025-11-20 11:55  FAfa_C++  阅读(9)  评论(0)    收藏  举报