二叉树基础题型(C++)

一、求二叉树的深度

//  1.广度优先搜索(BFS):
int maxDepth(TreeNode* root){
	queue<TreeNode*> q;
	if(root) q.push(root);
	int Tdepth = 0;
	while(!q.empty()){
		for(int i=q.size()-1;i>=0;i--){
			TreeNode* cur = q.front();
			q.pop();
			if(cur->left) q.push(cur->left);
			if(cur->right)q.push(cur->right);
}
		++Tdepth;
}
		return Tdepth;
}
//2.深度优先遍历/递归法(DFS):
int maxDepth(TreeNode* root){
	if(!root) return 0;
	return max(maxDepth(root->left),maxDepth(root->right))+1;
}

二、二叉搜索树中搜索某值

//1.递归法
TreeNode* searchBST(TreeNode* root, int val){
	if(!root) return NULL;
	if(root->val==val) return root;
	if(root->val>val){
		return searchBST(root->left,val);
}else{
		return searchBST(root->right,val);
}
}
//2.迭代法
TreeNode* searchBST(TreeNode* root, int val){
	while(root){
		if(val==root->val)return root;
		else if(val<root->val)root=root->left;
		else  root= root->right;
}
	return NULL;
}

三、前序/中序/后序遍历

先序遍历:
vector<int> res;
vector<int> preorderTraversal(TreeNode* root){
	stack<TreeNode*> st;
	TreeNode* p=root;
	while(p||!st.empty()){
		if(p){
			res.push_back(p->val);
			st.push(p);
			p=p->left;
		}else{
			p=st.top();
			st.pop();
			p=p->right;
		}	
}
		return res;
}
中序遍历:
vector<int> res;
vector<int> inorderTraversal(TreeNode* root){
	stack<TreeNode*> st;
	TreeNode* p = root;
	while(p||!st.empty()){
		if(p){
			st.push(p);
			p=p->left;
		}else{
			p=st.top();
			st.pop();
			res.push_back(p->val);
			p=p->right;
			}
		}
		return res;
}
后序遍历:
vector<int>res;
vector<int> postorderTraversal(TreeNode* root){
	stack<TreeNode*>st;
	TreeNode *p=root,*pre=NULL;
	while(p||!st.empty()){
		if(p){
			st.push(p);
			p=p->left;
		}else{
			p=st.top();
			if(p->right&&pre!=p->right){
				p=p->right;
			}else{
				res.push_back(p->val);
				st.pop();
				pre=p;
				p=NULL;
			}
		}
	}
	return res;
}

四、二叉搜索树中插入节点

递归法:
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if (!root) return new TreeNode(val);
        if (val < root->val) root->left = insertIntoBST(root->left, val);
        else root->right = insertIntoBST(root->right, val);        
        return root;        
    }
};
迭代法:
class Solution{
public TreeNode* insertIntoBST(TreeNode* root,int val){
	if(!root) return new TreeNode(val);
	TreeNode *p=new TreeNode(val);
	TreeNode *head=root;
	while(root){
		if(val<root->val){
			if(root->left){
				root=root->left;
			}else{
				root->left=p;
				break;
			}
		}else if(val>root->val){
			if(root->right){
				root=root->right;
			}else{
				root->right=p;
				break;
				}
			}
		}
		return head;
	}
};






}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树的层序遍历是指按照从上到下、从左到右的顺序访问二叉树的节点,并将节点按层级分组。对于给定的二叉树,我们可以使用队列来实现层序遍历。 以下是一个使用队列进行二叉树层序遍历的C++代码示例: ```c++ class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> result; queue<TreeNode*> que; if (root != nullptr) { que.push(root); } while (!que.empty()) { vector<int> temp; int length = que.size(); for (int i = 0; i < length; i++) { TreeNode* tempNode = que.front(); que.pop(); temp.push_back(tempNode->val); if (tempNode->left) { que.push(tempNode->left); } if (tempNode->right) { que.push(tempNode->right); } } result.push_back(temp); } return result; } }; ``` 以上代码实现了一个名为`levelOrder`的函数,该函数接受一个二叉树的根节点作为参数,并返回一个二维向量,表示二叉树的层序遍历结果。在函数内部,我们首先创建一个空的二维向量`result`用于存储结果,然后创建一个队列`que`用于辅助遍历。如果给定的二叉树不为空,我们将根节点入队。接下来,我们使用循环来遍历队列中的节点,并将节点的值存储到一个临时向量`temp`中。在遍历过程中,如果当前节点有左子节点,我们将左子节点入队;如果当前节点有右子节点,我们将右子节点入队。当遍历完一层后,我们将临时向量`temp`存储到结果向量`result`中,并继续下一层的遍历。最后,我们返回结果向量`result`。 希望以上解答对您有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [二叉树的层次遍历(C++)](https://blog.csdn.net/weixin_42817333/article/details/125110191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值