Skip to content

Commit 11f4cdc

Browse files
author
tanjiasheng
committed
Binary Tree Inorder Traversal
1 parent 3cbf76e commit 11f4cdc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
function inorderTraversal(root) {
13+
const result = [];
14+
15+
return traversal(root, result);
16+
};
17+
18+
function traversal(node, result) {
19+
if (!node) return result;
20+
if (node.left) {
21+
traversal(node.left, result)
22+
}
23+
24+
result.push(node.val)
25+
26+
if (node.right) {
27+
traversal(node.right, result)
28+
}
29+
30+
return result;
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
二叉树中序遍历递归解法

0 commit comments

Comments
 (0)