programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive
1.二叉树定义
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
2.遍历
a.递归先序:
//递归先序: 中左右。PS:中序-左中右,后序-左右中,调换cout的位置即可
void NLR(TreeNode* T)
{
if(T!=NULL){
cout<<T->val;
NLR(T->left);
NLR(T->right);
}
return;
}
PS:
递归变量传递的几种方式:
- 参数里,比如可以以此标记每个node的深度;
- return,适合做累计操作,例子:
int maxDepth(TreeNode *root) //求最大深度:反过来算+max,符合逻辑
{
return root == NULL ? : max(maxDepth(root -> left), maxDepth(root -> right)) + ;
} - 参数里加&,贯穿型变量。
b.DFS/非递归先序
//DFS-非递归先序:中左右
void depthFirstSearch(TreeNode* root){
stack<TreeNode *> nodeStack; //stack
nodeStack.push(root);
TreeNode *node;
while(!nodeStack.empty()){
node = nodeStack.top();
cout<<node->val; //action
nodeStack.pop();
if(node->right!=null){
nodeStack.push(node->right);
}
if(node->left!=null){
nodeStack.push(node->left);
}
}
}
c.BFS
//BFS
void BFS(TreeNode* root){
queue<TreeNode *> nodeQueue; //queue
nodeQueue.push(root);
TreeNode *node;
while(!nodeQueue.empty()){
node = nodeQueue.front();
cout<<node->val; //action
nodeQueue.pop();
if(node->left!=null){
nodeQueue.push(node->left);
}
if(node->right!=null){
nodeQueue.push(node->right);
}
}
}
变形,按层action:
int maxDepth(TreeNode* root) {
int res=;
if(root){
queue<TreeNode*> mq;
mq.push(root);
TreeNode* node;
while(!mq.empty()){
res++;
for(int i=,n=mq.size();i<n;i++){
node=mq.front();
mq.pop();
if(node->left!=NULL)
mq.push(node->left);
if(node->right!=NULL)
mq.push(node->right);
}
}
}
return res;
}
programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive的更多相关文章
- 257. Binary Tree Paths (dfs recurive & stack)
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- Binary Tree的3种非Recursive遍历
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- LeetCode Binary Tree Right Side View (DFS/BFS)
题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- BFS广度优先 vs DFS深度优先 for Binary Tree
https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...
随机推荐
- andorid人员文件上传服务器的搭建(tomcat中)
1.将.war文件复制到tomcat服务器webapps下,启动服务器即可 2.访问工程路径http://localhost:8080/FileUpload/index.jsp即可测试上传 3.测试成 ...
- 关于TP中U方法,在wamp中是绝对路径,在nginx中是相对路径?(坑)
这个问题已多次遇到,关于tp 框架 使用U 方法跳转, 在Nginx 服务器上可能会遇到路由跳转不过去前面带点(如:./xx) 解决这个问题,可以在tp的入口文件 index.php 里定义个常量 d ...
- HDU 6249 Alice’s Stamps(2017 CCPC-Final G题,DP)
题目链接 HDU 6249 题意 给定$m$个区间,在这些区间中选出不超过$k$个,求被覆盖的点的数量的最大值. 设$f[i][j]$表示选到第$i$个点并选了$j$个区间的时候能得到的最大答案. 处 ...
- BZOJ 3672 NOI 2014 购票
题面 Description 今年夏天,NOI在SZ市迎来了她30周岁的生日.来自全国 n 个城市的OIer们都会从各地出发,到SZ市参加这次盛会. 全国的城市构成了一棵以SZ市为根的有根树,每个城市 ...
- js判断是否是PC,IOS,Android客户端
写在前面 在项目中使用html5,需要针对不同的客户端浏览器有不一样的处理方式,这就需要对请求中的useragent进行分析,并进行处理. 一个例子 <%@ Page Language=&quo ...
- JavaScript 深克隆
深克隆 function judgeType(arg){//判断js数据类型 return Object.prototype.toString.call(arg).slice(8,-1); } fun ...
- PHP生成月历代码
<?php /* Function Written by Nelson Neoh @3/2004. For those who wants to utilize this code, p ...
- MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)
1.新建一个基于对话框的工程MyLink,在对话框中拖一个Static控件,ID可改为IDC_SLink. 2.在头文件中添加成员变量: private: CRect m_Rect; CFont* m ...
- antialiasing
Aliasing发生的原因在于,采样频率过低, 这样就无法重建更接近原来texture的图像, Temporal aliasing在时域上 采的texture 次数太少 不能重建真实 是帧与帧之间,s ...
- 新版本号的tlplayer for android ,TigerLeapMC for windows公布了
tlplayer for android 新版本号修正了图像倾斜等等问题,添加了动态水印功能. 支持hls(m3u8),http,rtsp,mms,rtmp等网络协议. 声明tlplayer 上的变速 ...