• 2024-12-15二叉树常见题目2
    [Algo]二叉树常见题目21.最近公共祖先LCABinaryTreeNode*LCA(BinaryTreeNode*root,BinaryTreeNode*a,BinaryTreeNode*b){if(root==nullptr||root==a||root==b)returnroot;BinaryTreeNode*l=LCA(root->left,a,b),*r=LCA(root->right,a,
  • 2024-12-14二叉树常见题目
    [Algo]二叉树常见题目1.层序遍历//1.层序遍历voidBFS(BinaryTreeNode*root){queue<BinaryTreeNode*>q;vector<BinaryTreeNode*>v;q.push(root);while(!q.empty()){while(!q.empty()){v.push_back(q.fro
  • 2024-12-06二叉树遍历
    [Algo]二叉树遍历二叉树节点类型定义:structBinaryTreeNode{intval;BinaryTreeNode*left;BinaryTreeNode*right;BinaryTreeNode(intx):val(x),left(nullptr),right(nullptr){}};1.前序遍历//1.非递归前序遍历二叉树//(1)弹出栈顶(2)