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?
递归解决方案
- class Solution {
- public:
- vector<int> res;
- void inorder(TreeNode* root){
- if(root == NULL) return;
- inorder(root->left);
- res.push_back(root->val);
- inorder(root->right);
- }
- vector<int> inorderTraversal(TreeNode *root) {
- inorder(root);
- return res;
- }
- };
递归中序遍历
非递归解决方案
- class Solution {
- public:
- vector<int> inorderTraversal(TreeNode *root) {
- vector<int> res;
- if(root == NULL) return res;
- stack<TreeNode *> nodeStack;
- TreeNode *current = root;
- while(!nodeStack.empty() || current ){
- if(current != NULL){
- nodeStack.push(current);
- current = current->left;
- }else{
- current = nodeStack.top();nodeStack.pop();
- res.push_back(current->val);
- current = current->right;
- }
- }
- return res;
- }
- };
非递归中序遍历
Leetcode Binary Tree Inorder Traversal的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode Binary Tree Inorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [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 ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
随机推荐
- python字符串中插入变量
- Faster-rnnlm代码分析1 - 词表构建,Nnet成员
https://github.com/yandex/faster-rnnlm Gdb ./rnnlm r -rnnlm model-good.faster -train thread.titl ...
- FP-Growth算法及演示程序
FP-Growth算法 FP-Growth(频繁模式增长)算法是韩家炜老师在2000年提出的关联分析算法,它采取如下分治策略:将提供频繁项集的数据库压缩到一棵频繁模式树(FP-Tree),但仍保留项集 ...
- Android之Adapter用法总结-(转)
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(List View,Grid Vie ...
- POJ 1840 Eqs 二分+map/hash
Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The co ...
- MyBatis入门案例 增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- OGNL表示式使用和值栈
另外值得参考博客:http://blog.csdn.net/resigshy/article/details/7560573 OGNL是Object Graphic Navigation Langua ...
- Null值的使用
- Almost Sorted Array
http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006 #include<iostream> ...
- super一些要点
package o6; class Grandparent { public Grandparent() { System.out.println("GrandParent Created. ...