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的更多相关文章

  1. 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 ...

  2. Binary Tree的3种非Recursive遍历

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 257. Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  4. the longest distance of a binary tree

    版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...

  5. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  6. LeetCode Binary Tree Right Side View (DFS/BFS)

    题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...

  7. (二叉树 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 ...

  8. (二叉树 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 ...

  9. 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 ...

随机推荐

  1. 02深入理解C指针之---指针类型和值

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 1.指针的类型: 可以在声明指针时,指定指针的类型,例如: (1)void *x  声 ...

  2. hdu 4857(好题,反向拓扑排序)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  3. springBoot 读取配置文件yml中的信息

    yml中自定义一些变量 var: analyze_url: test ocr_url: test microsoft_key: test 映射到类变量中 @Getter @Component publ ...

  4. Java平台下的gitignore文件

    *.bak*.txt*.vm.gitignore#svn.svn/# built application files*.apk*.ap_ # files for the dex VM*.dex # J ...

  5. Django中的form设置field的html属性

    在Django中无论何种field,都有一个widget的属性: class Field(object): widget = TextInput # Default widget to use whe ...

  6. MariaDB+Galera+Haproxy+Keepalived搭建集群

    1.MariaDB Galera Cluster介绍 MariaDB Galera Cluster是MariaDB同步多主机集群,它仅支持XtraDB/InnoDB存储引擎,在MySQLInnoDB存 ...

  7. gitbook简单安装和使用

    1.gitbook是用来写书的,支持markdown等 2.在线写 gitbook最新版地址:https://www.gitbook.com/account/ 免费用户只能有一个public和一个pr ...

  8. EasyMvc入门教程-图形控件说明(21)线形图+柱状图+饼形图

    本章将介绍一些基本但常用的图形:线型图,柱状图和饼形图. 以上三种图形对于的数据都是键值对数组,请参考第一个例子: @{ var data = new List<LineItem>(); ...

  9. 转: Syslog协议介绍

    转: http://liu-hliang.iteye.com/blog/827392 在网上搜的文章,写的很全乎.摘抄如下,供大家参考学习 1.介绍 在Unix类操作系统上,syslog广泛应用于系统 ...

  10. 全国天气预报数据API调用PHP示例

    本代码示例是基于PHP的聚合数据全国天气预报API服务请求的代码样例,使用前你需要: ①:通过https://www.juhe.cn/docs/api/id/39 申请一个天气预报API的appkey ...