代码随想录刷题记录-Day14

To Do List

  • DAY 13 回顾
  • 二叉树的理论知识的理解
  • 二叉树的递归遍历三部曲
  • DAY 14 总结

DAY 11 回顾

()


二叉树的理论知识的理解

(带补充)

二叉树的递归遍历三部曲(前序、中序、后序)

前序遍历

1
Difficulty: Easy

给你二叉树的根节点 root ,返回它节点值的 前序 遍历

Example 1:

img

1
2
Input: root = [1,null,2,3]
Output: [1,2,3]

Example 2:

1
2
Input: root = []
Output: []

Example 3:

1
2
Input: root = [1]
Output: [1]

Constraints:

  • The number of nodes in the tree is in the range [0, 100].

  • -100 <= Node.val <= 100

    Translated by zh-CN 树中节点数目在范围 [0, 100] 内

解法:

(JavaScript)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
let res = []
let recur = function(root){
if (root == null) return
res.push(root.val)
recur(root.left)
recur(root.right)
}
recur(root)
return res
};

PS:


后序遍历

1
Difficulty: Easy

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

Example 1:

img

1
2
Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2:

1
2
Input: root = []
Output: []

Example 3:

1
2
Input: root = [1]
Output: [1]

Constraints:

  • The number of the nodes in the tree is in the range [0, 100].

  • -100 <= Node.val <= 100

    Translated by zh-CN 树中节点数目在范围 [0, 100] 内

解法:

(JavaScript)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var postorderTraversal = function(root) {
let res = []
let recur = function(root){
if (root == null) return
recur(root.left)
recur(root.right)
res.push(root.val)
}
recur(root)
return res
};

PS:


中序遍历

1
Difficulty: Easy

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

Example 1:

img

1
2
Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2:

1
2
Input: root = []
Output: []

Example 3:

1
2
Input: root = [1]
Output: [1]

Constraints:

  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
Translated by zh-CN 树中节点数目在范围 [0, 100] 内
---

解法:

(JavaScript)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var inorderTraversal = function(root) {
let res = []
let recur = function(root){
if (root == null) return
recur(root.left)
res.push(root.val)
recur(root.right)
}
recur(root)
return res
};