题目地址: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. 毕业设计之zabbix之nginx状态监控

    监控脚本: [root@webone.quan.bbs ~]$vim /usr/local/zabbix/script/ngx_status.sh #!/bin/bash##************* ...

  2. Atom编辑器速查

    简介 Atom 是 Github 开源的文本编辑器,相当于半个IDE.其特点如下: (1)免费开源,多平台支持(Windows.Mac.Linux): (2)界面美观.现代化,使用舒适: (3)多文件 ...

  3. pyspider爬虫框架的安装和使用

    pyspider是国人binux编写的强大的网络爬虫框架,它带有强大的WebUI.脚本编辑器.任务监控器.项目管理器以及结果处理器,同时支持多种数据库后端.多种消息队列,另外还支持JavaScript ...

  4. ArrayList总结及部分源码分析

    ArrayList源码阅读笔记 1. ArrayList继承的抽象类和实现的接口 ArrayList类实现的接口 List接口:里面定义了List集合的基本接口,ArrayList进行了实现 Rand ...

  5. Scala(四)【集合基础入门】

    目录 一.Array 二. List 三.Set 四.Tuple 五.Map 一.Array package com.bigdata.scala.day01 /** * @description: 不 ...

  6. tomcat在eclipse上发布,Perference下的server找不到解决办法

    help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...

  7. 转 android design library提供的TabLayout的用法

    原文出处:http://chenfuduo.me/2015/07/30/TabLayout-of-design-support-library/ 在开发中,我们常常需要ViewPager结合Fragm ...

  8. ps精修

    1.磨皮方法: a,, 添加高斯模糊后,按住alt键新建图层蒙版,设置前景色为白色,用画笔在脸上雀斑的位置涂抹,注意脸轮廓位置不要涂抹.最后添加曲线提亮 b. 添加蒙尘和划痕后,后面上面的一样

  9. Virtual functions in derived classes

    In C++, once a member function is declared as a virtual function in a base class, it becomes virtual ...

  10. 【Linux】【Services】【Package】rpm包制作

    1. 概念 1.1. BUILD:源代码解压之后存放的位置 1.2. RPMS:制作完成之后的RPM包的存放位置,包括架构的子目录,比如x86,x86_64 1.3. SOURCES:所有的原材料都应 ...