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 ...
随机推荐
- Color.FromArgb()方法详解
关于颜色值的表示 常用的颜色值表示方式有两种,一种是10进制的RGB值表示,如(0,113,255),三个值分别表示(红,绿,蓝):一种是16进制的颜色码表示,如#ff3212.这两种形式在编程中都可 ...
- 前端优化 -- Combo Handler
Combo Handler来合并CSS/JS文件 背景 Combo Handler是Yahoo!开发的一个Apache模块,它实现了开发人员简单方便地通过URL来合并JavaScript和CSS文件, ...
- Codeforces 899 A.Splitting in Teams
A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input sta ...
- linux定时问题
手动运行java脚本,可以,但定时运行没效果 查看脚本是否赋予执行权限:若无则赋予可执行权限,若还是没用,则在脚本增添环境变量,百度搜索.
- Educational Codeforces Round 35 A. Nearest Minimums【预处理】
[题目链接]: Educational Codeforces Round 35 (Rated for Div. 2) A. Nearest Minimums time limit per test 2 ...
- Codeforces 785D Anton and School - 2(推公式+乘法原理+组合数学)
题目链接 Anton and School - 2 对于序列中的任意一个单括号对(), 左括号左边(不含本身)有a个左括号,右括号右边(不含本身有)b个右括号. 那么答案就为 但是这样枚举左右的()的 ...
- 纯手写Myatis框架
1.接口层-和数据库交互的方式 MyBatis和数据库的交互有两种方式: 使用传统的MyBatis提供的API: 使用Mapper接口: 2.使用Mapper接口 MyBatis 将配置文件中的每一个 ...
- 1219 骑士游历(棋盘DP)
1997年 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 设有一个n*m的棋盘(2≤n≤50,2≤m≤50),如 ...
- html中常用的标签小结
内容详细标签: <h1>~<h6>标题标签<pre>格式化文本<u>下划线(underline)<i>斜体字(italics)<cit ...
- php实现将人民币金额转大写的办法
class Num2Cny{ static $basical=array(0=>'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); static $advance ...