## 题目地址

https://leetcode.com/problems/binary-tree-inorder-traversal/

## 题目描述

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

Example:

``````Input: [1,null,2,3]
1
\
2
/
3

Output: [1,3,2]
``````

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

## 思路

``````class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> res;
inorder(root, res);
return res;
}
void inorder(TreeNode *root, vector<int> &res) {
if (!root) return;
if (root->left) inorder(root->left, res);
res.push_back(root->val);
if (root->right) inorder(root->right, res);
}
};
``````

``````// Non-recursion
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> res;
stack<TreeNode*> s;
TreeNode *p = root;
while (p || !s.empty()) {
while (p) {
s.push(p);
p = p->left;
}
p = s.top(); s.pop();
res.push_back(p->val);
p = p->right;
}
return res;
}
};
``````

``````class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> s;
TreeNode *p = root;
while (!s.empty() || p) {
if (p) {
s.push(p);
p = p->left;
} else {
p = s.top(); s.pop();
res.push_back(p->val);
p = p->right;
}
}
return res;
}
};
``````

A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node ( if it exists), and all left child pointers that would normally be null point to the inorder predecessor of the node.

1. 初始化指针 cur 指向 root

2. 当 cur 不为空时

- 如果 cur 没有左子结点

a) 打印出 cur 的值

b) 将 cur 指针指向其右子节点

- 反之

将 pre 指针指向 cur 的左子树中的最右子节点

* 若 pre 不存在右子节点

a) 将其右子节点指回 cur

b) cur 指向其左子节点

* 反之

a) 将 pre 的右子节点置空

b) 打印 cur 的值

c) 将 cur 指针指向其右子节点

``````class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> res;
if (!root) return res;
TreeNode *cur, *pre;
cur = root;
while (cur) {
if (!cur->left) {
res.push_back(cur->val);
cur = cur->right;
} else {
pre = cur->left;
while (pre->right && pre->right != cur) pre = pre->right;
if (!pre->right) {
pre->right = cur;
cur = cur->left;
} else {
pre->right = NULL;
res.push_back(cur->val);
cur = cur->right;
}
}
}
return res;
}
};
``````

### 思路2

• 从根节点开始，先将根节点压入栈

• 然后再将其所有左子结点压入栈，取出栈顶节点，保存节点值

• 再将当前指针移到其右子节点上，若存在右子节点，则在下次循环时又可将其所有左子结点压入栈中， 重复上步骤

## 关键点解析

• 二叉树的基本操作（遍历）

• 如果非递归的话利用栈来简化操作

• 如果数据规模不大的话，建议使用递归

• 递归的问题需要注意两点，一个是终止条件，一个如何缩小规模

1. 终止条件，自然是当前这个元素是null（链表也是一样）

2. 由于二叉树本身就是一个递归结构， 每次处理一个子树其实就是缩小了规模，
难点在于如何合并结果，这里的合并结果其实就是`left.concat(mid).concat(right)`,
mid是一个具体的节点，left和right`递归求出即可`

## 代码

• 语言支持：JS，C++，Python3, Java

JavaScript Code：

``````/*
* @lc app=leetcode id=94 lang=javascript
*
* [94] Binary Tree Inorder Traversal
*
* https://leetcode.com/problems/binary-tree-inorder-traversal/description/
*
* algorithms
* Medium (55.22%)
* Total Accepted:    422.4K
* Total Submissions: 762.1K
* Testcase Example:  '[1,null,2,3]'
*
* Given a binary tree, return the inorder traversal of its nodes' values.
*
* Example:
*
*
* Input: [1,null,2,3]
* ⁠  1
* ⁠   \
* ⁠    2
* ⁠   /
* ⁠  3
*
* Output: [1,3,2]
*
* Follow up: Recursive solution is trivial, could you do it iteratively?
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
*     this.val = val;
*     this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
// 1. Recursive solution
// if (!root) return [];
// const left = root.left ? inorderTraversal(root.left) : [];
// const right = root.right ? inorderTraversal(root.right) : [];
// return left.concat([root.val]).concat(right);

// 2. iterative solutuon
if (!root) return [];
const stack = [root];
const ret = [];
let left = root.left;

let item = null; // stack 中弹出的当前项

while(left) {
stack.push(left);
left = left.left;
}

while(item = stack.pop()) {
ret.push(item.val);
let t = item.right;

while(t) {
stack.push(t);
t = t.left;
}
}

return ret;

};

``````

C++ Code：

``````/**
* 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<TreeNode*> s;
vector<int> v;
while (root != NULL || !s.empty()) {
for (; root != NULL; root = root->left)
s.push_back(root);
v.push_back(s.back()->val);
root = s.back()->right;
s.pop_back();
}
return v;
}
};
``````

Python Code:

``````# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
"""
1. 递归法可以一行代码完成，无需讨论；
2. 迭代法一般需要通过一个栈保存节点顺序，我们这里直接使用列表
- 首先，我要按照中序遍历的顺序存入栈，这边用的逆序，方便从尾部开始处理
- 在存入栈时加入一个是否需要深化的参数
- 在回头取值时，这个参数应该是否，即直接取值
- 简单调整顺序，即可实现前序和后序遍历
"""
# 递归法
# if root is None:
#     return []
# return self.inorderTraversal(root.left)\
#     + [root.val]\
#     + self.inorderTraversal(root.right)
# 迭代法
result = []
stack = [(1, root)]
while stack:
go_deeper, node = stack.pop()
if node is None:
continue
if go_deeper:
# 左右节点还需继续深化，并且入栈是先右后左
stack.append((1, node.right))
# 节点自身已遍历，回头可以直接取值
stack.append((0, node))
stack.append((1, node.left))
else:
result.append(node.val)
return result
``````

Java Code:

• recursion
``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
inorder(root);
return res;
}

public void inorder (TreeNode root) {
if (root == null) return;

inorder(root.left);

inorder(root.right);
}
}
``````
• iteration
``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<> ();
Stack<TreeNode> stack = new Stack<> ();

while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
root = root.right;
}
return res;
}
}
``````

评论
0 评论