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 ...
随机推荐
- .NET CMS系统--pageAdmin 模板样式设置
修改方法: 表单模型管理 - 数据表管理 - 模型管理 修改产品详情页
- Factory Method 和AbstractFactory
对应慕课视频的连接:https://www.imooc.com/video/5316 1,工厂模式的应用场景 有一组类似的对象需要被创建 在编码时不能预见需要被创建哪种类的实例 在系统需要考虑扩展性的 ...
- SQLServer出现不允许保存更改的问题解决
如图所示: 解决方法: [工具]->[选项]
- [js]数组栈和队列操作
写在前面 在项目中,对数组的操作还是比较常见的,有时候,我们需要模拟栈和队列的特性才能实现需求,这里记录一下这个知识点. 栈 栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一 ...
- 修改密码删除登陆态,那其他正在登陆的app怎么办?
修改密码删除登陆态,那其他正在登陆的app怎么办?
- Learn How To Attach PL/SQL Library In Oracle Forms
To attach a PL/SQL library in the Oracle Forms follow the following steps:1. Click on Attached Libra ...
- 报错:OpenCV Error: Assertion failed (src.size() == dst.size() && src.type() == dst.ty pe()) in unknown function, file ..……
在用cvDilate函数的时候,老是导致程序中断,报错如下: OpenCV Error: Assertion failed (src.size() == dst.size() && s ...
- Solidworks如何改变零件颜色
如图所示装配体有三个零件,现在我想把移动件的颜色变成红色 鼠标左键单击要改变颜色的零件(这里点击"移动件"),然后在弹出的菜单中选择颜色,最后点击"编辑颜色" ...
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》移植到android手机
说明:这里是借鉴:晓风残月前辈的博客.他是将泰然网的跑酷教程.用cocos2d-x 2.X 版本号重写的,眼下我正在学习cocos2d-X3.0 于是就用cocos2d-X 3.0重写,并做相关笔记 ...
- vue修改数组元素方法
示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF- ...