题目地址:https://leetcode.com/problems/binary-tree-inorder-traversal/

题目描述

Given a binary tree, return the inorder traversal of its nodes’ values.

For example:

Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

解题方法

递归

题目要求中序遍历。这里给递归解法和遍历解法,要背会哦~

递归方法比较简单,直接按照左子树->该节点->右子树的顺序遍历即可。

如果有不明白的,直接看官方解答,有图文:https://leetcode.com/problems/binary-tree-inorder-traversal/solution/

Python代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
answer = []
def inorder(root):
if root == None:
return None
if root.left != None:
inorder(root.left)
answer.append(root.val)
if root.right != None:
inorder(root.right)
inorder(root)
return answer

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) {
inOrder(root);
return res;
}
private:
vector<int> res;
void inOrder(TreeNode* root) {
if (!root) return;
inOrder(root->left);
res.push_back(root->val);
inOrder(root->right);
}
};

迭代

迭代解法需要用到栈,这个方法确实比递归难得多了。

我们先把节点所有的左节点放入栈中,然后开始出栈,每次出栈都把栈中的元素放入到结果中,并且把这个结果的右孩子放入栈中。

因此,这里的遍历顺序先沿着最左方向到达最左下角的孩子,然后每次弹出来一个节点,把该节点的值放入结果中,并开始处理该节点的右子树。

Python代码如下。

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
stack = []
answer = []
while True:
while root:
stack.append(root)
root = root.left
if not stack:
return answer
root = stack.pop()
answer.append(root.val)
root = root.right

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) {
stack<TreeNode*> s;
vector<int> res;
TreeNode* p = root;
while (!s.empty() || p) {
if (p) {
s.push(p);
p = p->left;
} else {
TreeNode* t = s.top(); s.pop();
res.push_back(t->val);
p = t->right;
}
}
return res;
}
};

日期

2018 年 2 月 8 日
2018 年 12 月 11 日 —— 双十一已经过去一个月了,真快啊。。

【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)的更多相关文章

  1. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  2. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  3. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  4. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  5. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  6. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  7. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  9. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

随机推荐

  1. 金蝶EAS——登录某个数据中心门户时报错“获取用户相关信息失败!请查看服务器日志,并确认是否数据库设置错误或者版本不匹配!”

    登录服务器后台,查看金蝶BOS控制台,选择数据中心中的目标数据中心,点击测试连接,提示报错如下: 说明是数据库问题,需要登录数据库服务器去检查数据库.详细操作见:

  2. Python time&datetime模块

    1.time&datetime模块 time&datetime是时间模块,常用以处理时间相关问题 time.time() #返回当前时间的时间戳timestamp time.sleep ...

  3. URLDNS分析

    学习了很久的Java基础,也看了很多的Java反序列化分析,现在也来分析学习哈最基础的URLDNS反序列化吧. Java反序列化基础 为了方便数据的存储,于是乎有了现在的Java序列化于反序列化.序列 ...

  4. POLLOUT/POLLINT事件触发测试

    一般poll使用过程中都是检测POLLIN事件,表示描述符有事件可以处理了,需要我们处理.对POLLOUT事件触发的方式相对较少,网上也有很多对此触发的疑问,利用实际项目中用的一个用法,下面做了个测试 ...

  5. Vue3 中有哪些值得深究的知识点?

    众所周知,前端技术一直更新很快,这不 vue3 也问世这么久了,今天就来给大家分享下vue3中值得注意的知识点.喜欢的话建议收藏,点个关注! 1.createApp vue2 和 vue3 在创建实例 ...

  6. 漏洞检测方法如何选?详解源代码与二进制SCA检测原理

    摘要:本文探讨的是SCA具体的检测原理,源代码SCA检测和二进制SCA检测有哪些相同点和不同点,在进行安全审计.漏洞检测上各自又有什么样的优势和适用场景. 本文分享自华为云社区<源代码与二进制文 ...

  7. A Child's History of England.20

    CHAPTER 7 ENGLAND UNDER HAROLD THE SECOND, AND CONQUERED BY THE NORMANS Harold was crowned King of E ...

  8. Python计算期权隐含波动率

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. Black-Scholes 将期权价格描述为标的价格.行权价.无风险利率.到期时间和波动性的函数.  V ...

  9. ViewStub应用

    在开发应用程序的时候,会遇到这样的情况,在运行时动态的根据条件来决定显示哪个View或哪个布局,可以把可能用到的View都写在上面,先把他们的可见性设置为View.GONE,然后在代码中动态的更改它的 ...

  10. adb命令对app进行测试

    1.何为adb adb android  debug  bridge ,sdk包中的工具,将Platform-tooks 和tools  两个路径配置到环境变量中 2.SDK下载链接:http://t ...