LeetCode 94. Binary Tree Inorder Traversal--二叉树中序遍历--递归,迭代--C++,Python解法
题目地址:Binary Tree Inorder Traversal - LeetCode
Given a binary tree, return the inorder traversal of its nodes’ values.
Example:
Input: [1,null,2,3]1\2/3Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
经典的二叉树中序遍历,有迭代和递归2种做法,递归的比较简单。
Python解法如下:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def inorderTraversal(self, root: TreeNode) -> List[int]:def helper(root, l):if root is None:returnhelper(root.left,l)l.append(root.val)helper(root.right,l)l = []if root is None:return lhelper(root, l)return l
C++解法如下:
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {public:vector<int> l;void helper(TreeNode *root) {if (root == nullptr) {return;}helper(root->left);l.push_back(root->val);helper(root->right);}vector<int> inorderTraversal(TreeNode *root) {l.clear();helper(root);return l;}
};
递归的解法总是比迭代简单,迭代解法如下:
Python解法如下:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution:def inorderTraversal(self, root: TreeNode) -> List[int]:if root == None:return []l = []res = []curr = rootwhile curr != None or l != []:while curr is not None:l.append(curr)curr = curr.leftcurr = l.pop(-1)res.append(curr.val)curr = curr.rightreturn res
时间复杂度为O(n),空间复杂度为O(1)。
C++解法如下:
/*** Definition for a binary tree node.* 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> result;stack<TreeNode *> l;if (root == nullptr) {return result;}TreeNode *curr = root;while (curr != nullptr || !l.empty()) {while (curr != nullptr) {l.push(curr);curr = curr->left;}curr = l.top();l.pop();result.push_back(curr->val);curr = curr->right;}return result;}
};
LeetCode 94. Binary Tree Inorder Traversal--二叉树中序遍历--递归,迭代--C++,Python解法相关推荐
- 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal
LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...
- LeetCode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- 94. Binary Tree Inorder Traversal二叉树的中序遍历
网址:https://leetcode.com/problems/binary-tree-inorder-traversal/ 参考:https://leetcode.com/problems/bin ...
- 94. Binary Tree Inorder Traversal 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3]1\2/3输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 二叉树遍历 不太想用递归,试试循环吧 ...
- [LeetCode]:94:Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- leetcode[94]Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [swift] 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
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* ...
最新文章
- 基于fpga的数码管动态扫描电路设计_【至简设计案例系列】基于FPGA的密码锁设计(altera版)...
- php追加编译imagick
- slave-pxc后GTID不一致
- 实习随记-不定时更新
- F - 阿汤的疑惑(模拟取余+分解质因数)
- java 监听器实现原理
- 2-5-666:放苹果
- pkuseg:一个多领域中文分词工具包
- 额外参数_Pytorch获取模型参数情况的方法
- 单片机音乐芯片c语言程序,51单片机驱动MT8880双音频接收发送芯片C语言程序分享...
- paramiko上传下载
- 百度举办区块链论坛,携手多伙伴加速区块链产业落地
- this installer requires you to restart your system to finish installing Microsoft VC
- 头文件<ctype.h>
- 极客大挑战--PHP
- Python股票基金数据分析 爬虫 基金对比 Django框架 数据可视化
- 易语言-post登陆百度
- 地铁三号线 - 为什么哭的时候总是叫我带娃?
- JavaScript 三座大山
- exynos 4412
热门文章
- Nat. Mach. Intell.|从局部解释到全局理解的树模型
- 如何使用ggplot2绘制左右分布的柱状图
- 在线作图|你不知道的绘制带聚类树的堆叠柱状图的方法
- 零基础入门学习Python(11)-列表(3)
- 扩增子分析解读4去嵌合体,非细菌序列,生成代表性序列和OTU表
- seaborn使用violinplot函数可视化分组小提琴图(Grouped Violinplot with Seaborn violinplot)并保存可视化结果
- R语言使用str_replace函数和str_replace_all函数替换字符串中匹配到的模式:str_replace函数替换第一个匹配到的字符串、str_replace_all函数替换所有匹配到的
- R语言ggplot2可视化:将dataframe和数据列名称传递给函数通过函数进行ggplot2可视化输出
- B树和B+树分别是什么?区别在哪里?MySQL使用的是哪一种树?
- R语言可视化包ggplot2包调整线条粗细实战(Adjust Line Thickness)