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. why not ovp protection ?

    HW MSM8917 PM8937 PMI8940 Question : Recently, I connect usb cable with 10V to the phone. Why does t ...

  2. JavaScript-性能优化,函数节流(throttle)与函数去抖(debounce)

    我在写一个类似百度搜索框的自动提示功能时候,使用了AJAX+keydown事件.调试时候我发现,当在搜索框中输入文字的时候,控制台在不停发送AJAX.这在本地服务器测试还好,如果我把它拿到运行环境,很 ...

  3. C结构体struct用法小结

    结构体和int,float等类型一样是一种常用的类型,它是由各种基本数据类型构成,通常包含有struct关键字,结构体名,结构体成员,结构体变量. 一.结构体定义 通常有3种定义方式,以例子方式表示: ...

  4. Codeforces Round #464 (Div. 2) A. Love Triangle[判断是否存在三角恋]

    A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. dhlin-vim-wiki

    记录vim中常用的几个操作 入门指南 $ vimtutor vim中是区分大小写 vim中移动光标 h 向左移动 j 向下移动 k 向上移动 l 向右移动 其实使用方向键也是能移动的,但是熟悉后再一些 ...

  6. Java并发编程,深度探索J.U.C - AQS

    java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.U.C 的核心. CountdownLatch 用来控制一个线程等待多个线程. 维护了一个计数器 cnt ...

  7. zoj 2615 Cells 栈的运用

    题目链接:ZOJ - 2615 Scientists are conducting research on the behavior of a newly discovered Agamic Cell ...

  8. 转:Maven项目中获取classpath和资源文件的路径

    假设资源文件放在maven工程的 src/main/resources 资源文件夹下,源码文件放在 src/main/java/下, 那么java文件夹和resources文件夹在运行时就是class ...

  9. IIS 7 Access to the path ‘c:\windows\system32\inetsrv\’ is denied

    https://randypaulo.wordpress.com/2011/09/13/iis-7-access-to-the-path-cwindowssystem32inetsrv-isdenie ...

  10. (持续集成)win7上部署Jenkins+MSBuild+Svn+SonarQube+SonarQube Scanner for MSBuild (第二发)

    这一篇进入实战,走起.... 登录jenkins,如下图 点击上图中的“新建”按钮,进入下图 输入项目名称,选择“构建一个自由风格的软件项目”即可,点击“ok”,跳转到下图 svn源代码管理(选择代码 ...