【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树,返回他的中序遍历的节点的values。
例如:
给定一个二叉树 {1,#,2,3}
,
1
\
2
/
3
返回 [1,3,2].
笔记:
1
/ \
2 3
/
4
\
5
上面的二叉树可以序列化为"{1,2,3,#,#,4,#,#,5}"
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1
\
2
/
3
return [1,3,2]
.
Note: Recursive solution is trivial, could you do it iteratively?
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}"
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; void inorder(TreeNode *root, vector<int> &path) vector<int> inorderTraversal(TreeNode *root) // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); PrintTree(pNodeA1); vector<int> ans = inorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) DestroyTree(pNodeA1); |
输出结果:
2.非递归实现
根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:
对于任一结点P,
1)若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;
2)若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的P置为栈顶结点的右孩子;
3)直到P为NULL并且栈为空则遍历结束
test.cpp:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; //非递归中序遍历 // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); PrintTree(pNodeA1); vector<int> ans = inorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) DestroyTree(pNodeA1); |
输出结果:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_ struct TreeNode TreeNode *CreateBinaryTreeNode(int value); #endif /*_BINARY_TREE_H_*/ |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include <iostream>
#include <cstdio> #include "BinaryTree.h" using namespace std; /** //创建结点 return pNode; //连接结点 //打印节点内容以及左右子结点内容 if(pNode->left != NULL) if(pNode->right != NULL) printf("\n"); //前序遍历递归方法打印结点内容 if(pRoot != NULL) if(pRoot->right != NULL) void DestroyTree(TreeNode *pRoot) delete pRoot; DestroyTree(pLeft); |
【遍历二叉树】02二叉树的中序遍历【Binary Tree Inorder Traversal】的更多相关文章
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- BestCoder Round #63 (div.2)
感觉有些无聊的比赛. A 暴力枚举下就行 B 简单的dp,但是wa了一发后就去先把C做了,然后发现如果输入的100个数,是如1,2,3,4,...,100,然后k=50,个数为c(100,50).果断 ...
- swagger api 文档框架
<其他教程:https://www.cnblogs.com/FlyAway2013/p/7510279.html> 先看看swagger的生态使用图: 其中,红颜色的是swaggger官网 ...
- 浅谈<持续集成、持续交付、持续部署>(一)
谈谈持续集成,持续交付,持续部署之间的区别 经常会听到持续集成,持续交付,持续部署,三者究竟是什么,有何联系和区别呢? 假如把开发工作流程分为以下几个阶段: 编码 -> 构建 -> 集 ...
- 我的Android进阶之旅------>Android自定义窗口标题实例
该实例的功能比较简单,但是通过该实例的扩展可以在自定义标题中做出菜单导航等实用的功能,为了实现自定义窗口标题,需要做以下几个步骤: 1.给自定义标题提供一个界面 2.将自定义标题应用给Activity ...
- ABAP--关于字符串String到XString XString to String转换代码
转自http://guanhuaing.iteye.com/blog/1498891 代码如下 report zrich_0001. data: s type string, h(1) type x, ...
- 牛客小白月赛1 E 圆与三角形 【数学】
题目链接 https://www.nowcoder.com/acm/contest/85/E 思路 在三角形中,这一串东西的值恒为1 又 SIN A 的最大值 为1 所以 这串式子的最大值 就是 r ...
- hadoop1.2.1 datanode 由于权限无法启动 expected: rwxr-xr-x
/************************************************************ STARTUP_MSG: Starting DataNode STARTUP ...
- c的详细学习(6)函数
根据模块化程序设计的原则,一个较大的程序一般要分为若干个小模块,每个模块实现一个比较简单的功能.在c语言中,函数是一个基本的程序模块. (1)函数的基本概念: 1)基本介绍: 任何一个 ...
- 《程序员代码面试指南》第二章 链表问题 删除中间节点和a/b处节点
题目 例如 1-2-3-4 删除2,1-2-3-4-5 删除3 例如 a=1,b =2 java代码 /** * @Description:删除中间节点和a/b处节点 * @Author: lizho ...
- origin与referer的区别
referer显示来源页面的完整地址,而origin显示来源页面的origin: protocal+host,不包含路径等信息,也就不会包含含有用户信息的敏感内容 referer存在于所有请求,而or ...