[Leetcode] Binary tree inorder traversal二叉树中序遍历
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}".
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- vector<int> inorderTraversal(TreeNode *root)
- {
- vector<int> res;
- stack<TreeNode *> stk;
- while(root|| !stk.empty())
- {
- if(root) //将左孩子不停地压入栈
- {
- stk.push(root);
- root=root->left;
- }
- else //到最后先访问节点本身,然后转向其右孩子
- {
- root=stk.top();
- res.push_back(root->val);
- stk.pop();
- root=root->right;
- }
- }
- return res;
- }
- };
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- vector<int> inorderTraversal(TreeNode *root)
- {
- vector<int> res;
- inorderTrav(root,res);
- return res;
- }
- void inorderTrav(TreeNode *root,vector<int> &res)
- {
- if(root==NULL) return;
- inorderTrav(root->left,res);
- res.push_back(root->val);
- inorderTrav(root->right,res);
- }
- };
步骤:
1. 如果当前节点的左孩子为空,则输出当前节点并将其右孩子作为当前节点。
2. 如果当前节点的左孩子不为空,在当前节点的左子树中找到当前节点在中序遍历下的前驱节点。
a) 如果前驱节点的右孩子为空,将它的右孩子设置为当前节点。当前节点更新为当前节点的左孩子。
b) 如果前驱节点的右孩子为当前节点,将它的右孩子重新设为空(恢复树的形状)。输出当前节点。当前节点更新为当前节点的右孩子。
3. 重复以上1、2直到当前节点为空。
具体分析过程见AnnieKim的博客
- class Solution
- {
- public:
- vector<int> inorderTraversal(TreeNode* root)
- {
- vector<int> vec;
- TreeNode* prev=NULL;
- while(root !=NULL)
- {
- if(root->left==NULL)
- {
- vec.push_back(root->val);
- root=root->right;
- }
- else
- {
- prev=root->left;
- while(prev->right !=NULL&&prev->right !=cur)
- {
- prev=prev->right;
- }
- //关键在于前节点右孩子不存在时的处理和root节点的回溯
- if(prev->right==NULL)
- {
- prev->right=root;
- root=root->left;
- }
- else
- {
- prev->right=NULL;
- vec.push_back(root->val);
- root=root->right;
- }
- }
- }
- return vec;
- }
- };
[Leetcode] Binary tree inorder traversal二叉树中序遍历的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- WPF Style Setter use a TemplateBinding?
<Style TargetType="{x:Type local:ImageButton}"> <Setter Property="Horizontal ...
- 关联分析FPGrowth算法在JavaWeb项目中的应用
关联分析(关联挖掘)是指在交易数据.关系数据或其他信息载体中,查找存在于项目集合或对象集合之间的频繁模式.关联.相关性或因果结构.关联分析的一个典型例子是购物篮分析.通过发现顾客放入购物篮中不同商品之 ...
- 第二篇 Flask基础篇之(闪现,蓝图,请求扩展,中间件)
本篇主要内容: 闪现 请求扩展 中间件 蓝图 写装饰器,常用 functools模块,帮助设置函数的元信息 import functools def wrapper(func): @functools ...
- Angualr6访问API
参照 草根专栏- ASP.NET Core + Ng6 实战: https://v.qq.com/x/page/a0769armuui.html 1.environment.ts 添加apiUrlBa ...
- nginx web服务器的安装使用
nginx是一个web服务器(高性能web服务器),类似于apache服务器和iis服务器,由于nginx服务器具有轻量级高并发的特点,目前nginx的使用已经超越了apache. nginx介绍:n ...
- Java标签实现分页
Java实现标签分页 最近为了开发一个网站,里面要用分页功能,但是之前很少自己写分页标签,又不想用现成框架.所以自己参考了些资料,写了个分页例子测试了一下. 代码主要分为三个类: PageTag 分页 ...
- 你真的了解JAVA里的String么
Java中String类细节问题 (考察点Java内存分配问题) 1. String str1 = "abc"; System.out.println(str1 == &quo ...
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
- 自测之Lesson15:TCP&UDP网络编程
题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...