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?
说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iteratively)
2)时间复杂度 :O(n),空间复杂度:O(n)
实现:
一、递归
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/*recursive*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> root_vec;
vector<int> left_vec;
vector<int> right_vec;
if(root==nullptr) return root_vec;
if(root->left!=nullptr) left_vec=inorderTraversal(root->left);
root_vec.push_back(root->val);
if(root->right!=nullptr) right_vec=inorderTraversal(root->right);
left_vec.insert(left_vec.end(),root_vec.begin(),root_vec.end());
left_vec.insert(left_vec.end(),right_vec.begin(),right_vec.end());
return left_vec;
}
};
二、非递归
根据中序遍历的顺序,先访问左子树,再访问根节点,后访问右子树,而对于每个子树来说,又按照同样的访问顺序进行遍历,非递归的实现思路如下:
对于任一节点P,
1)若P的左孩子不为空,则将P入栈并将P的左孩子置为当前节点,然后再对当前节点进行相同的处理;
2)若P的左孩子为空,则输出P节点,而后将P的右孩子置为当前节点,看其是否为空;
3)若不为空,则重复1)和2)的操作;
4)若为空,则执行出栈操作,输出栈顶节点,并将出栈的节点的右孩子置为当前节点,看起是否为空,重复3)和4)的操作;
5)直到当前节点P为NULL并且栈为空,则遍历结束。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/*iteratively*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
stack<TreeNode *> inorder_stack;
TreeNode * p=root;
vector<int> inorder_vec;
if(p==nullptr) return inorder_vec;//若为空树,则返回空vector
while(p||!inorder_stack.empty())
{
if(p->left!=nullptr)//若左节点不空,当前节点进栈,并使右孩子为当前节点,继续判断
{
inorder_stack.push(p);
p=p->left;
}
else //如果左孩子为空,则输出当前节点,并将其右孩子设为当前节点,看其是否为空
{
inorder_vec.push_back(p->val);
p=p->right;
//如果为空,且栈不空,则将栈顶节点出栈,并输出该节点,
//同时将它的右孩子设为当前节点,继续判断,直到当前节点不为空
while(!p&&!inorder_stack.empty())
{
p=inorder_stack.top();
inorder_vec.push_back(p->val);
inorder_stack.pop();
p=p->right;
}
}
}
return inorder_vec; }
};
leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
随机推荐
- C# JabLib系列之如何保证只运行一个应用程序的实现
保证只运行一个应用程序的C#实现: using System;using System.Collections.Generic;using System.Linq;using System.Windo ...
- 在WPF程序中使用摄像头兼谈如何使用AForge.NET控件(转)
前言: AForge.NET 是用C#写的一个关于计算机视觉和人工智能领域的框架,它包括图像处理.神经网络.遗传算法和机器学习等.在C#程序中使用摄像头,我习惯性使用AForge.NET提供的类库.本 ...
- Javascript里,想把一个整数转换成字符串,字符串长度为2
Javascript里,想把一个整数转换成字符串,字符串长度为2. 想把一个整数转换成字符串,字符串长度为2,怎么弄?比如 1 => "01"11 => " ...
- CloudStack 4.2 与CloudStack 4.1二级存储API发生变化
CloudStack 4.1查看二级存储 http://192.168.150.16:8080/client/api?command=listHosts&response=json&s ...
- 有关于CSS的面试题和练习
如果你处在一个需要面试别人有关于CSS方面技巧和知识,一时想起来还真有点难.我把我能想出来的整理在一起,提供给大家参考. 练习要做 正如他们说的,大家实际工作很重要.当然,大家通过工作练习更为重要.这 ...
- [CentOS]CentOS/RedHat/Fedora的Proxy设定(yum,wget,,rpm)
yum 「/etc/yum.conf」 proxy=http://proxy.xxx.com:8080/ wget 「/etc/wgetrc」 http_proxy=http://proxy.xxx. ...
- 【M34】如何在同一个程序中结合C++和C
1.C++和C混合编程的时候,需要考虑产生的目标文件的兼容性. 2.名称重整,为什么要搞出名称重整? 连接器要求所有方法名必须独一无二.对于C语言,没问题.C++支持过载,也就是方法名相同,形参表不同 ...
- AR增强现实 Augmented Reality
增强现实(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上对应图像的技术,这样的技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动.这样的技术最早于19 ...
- 清除SQL Server 2008中登陆时的历史记录
win7 在地址栏直接输入下面路径,删除SqlStudio.bin文件%AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell
- C++面向对象的编程
C++面向对象的编程 目录 对C++面向对象编程的理解 声明和定义类 声明和定义 构造函数 析构函数 静态成员和静态变量 类实例化对象 对象的浅复制和深复制 继承 单继承 多继承 虚函数 类模板 其他 ...