algorithm-base/animation-simulation/二叉树/二叉树基础.md

479 lines
18 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
?
.......
``,````
PS
****
![image](https://cdn.jsdelivr.net/gh/tan45du/test@master/image.1ein9cz4oips.png)
> Github
##
> n n >= 0 n = 0 ,
``
- Root
- n > 1 m m > 0`` T1T2........Tm
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/二叉树.6w6xnvay3v40.png)
``
1 m `` T1,T2........Tm
T1 234567T289
T1 绿 T2
m ``
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/幻灯片1.3wt8kt6ewj20.PNG)
(A) , (B) , (C) , (D) (A) , (B) C, (D) (C) , (D)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/幻灯片2.4gvv5tql9cw0.PNG)
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/幻灯片3.17o6v5lqd9xc.PNG)
##
``
``
````
- 2 012
- ,
-
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/二叉树举例.1mavhkdbs8xs.png)
5 BC 5
##
``
###
````,.
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/image.2k1tlbtywzu0.png)
(B) ,
###
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/image.2f3i4soptvi8.png)
ABCD
###
,,,.
,.
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/image.6u1n2j3jmu80.png)
, i , 2 , , , ,, , .
##
,
,,
** 4 ?**
.
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/微信截图_20210223223621.3juf4t4hc9a0.png)
1 1 , 4 2 2 3
i , ` 2*i `, , 0 2*i+1 , 2*i+1 i/2 `` ` `
,,``?
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/image.780as9g3ofs0.png)
2\*i ,,,,
,,
,.
, , ,
val , left , right .
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/image.2m6tju8ruoo0.png)
1, 2, 3, 4, 5, 6, 7 使,.
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/二叉树链式存储.3cctqhi5ll40.png)
****
```java
public class BinaryTree {
int val;
BinaryTree left;
BinaryTree right;
BinaryTree() {}
BinaryTree(int val) { this.val = val; }
BinaryTree(int val, BinaryTree left, BinaryTree right) {
this.val = val;
this.left = left;
this.right = right;
}
}
```
, ``, , , .
,,
,, ,.
##
`,访`,使访访.
, , , , .
###
, ,`,,`.
,
![](https://img-blog.csdnimg.cn/20210504155755565.gif)
**: leetcode 144. **
**()**
```java
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> arr = new ArrayList<>();
preorder(root,arr);
return arr;
}
public void preorder(TreeNode root,List<Integer> arr) {
if (root == null) {
return;
}
arr.add(root.val);
preorder(root.left,arr);
preorder(root.right,arr);
}
}
```
: O(n) : O(n) , O(logn), O(n)
, ,
###
, `,, , `
, .
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/中序遍历.7gct7ztck8k0.gif)
**: leetcode 94 **
**()**
```java
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
inorder(root, res);
return res;
}
public void inorder (TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
inorder(root.left, res);
res.add(root.val);
inorder(root.right, res);
}
}
```
: O(n) : O(n)
###
,` , , , `.
,,.
![](https://cdn.jsdelivr.net/gh/tan45du/test@master/photo/后序遍历.2bx6qccr1q1w.gif)
**: leetcode 145 **
**()**
```java
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
postorder(root,res);
return res;
}
public void postorder(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
postorder(root.left, res);
postorder(root.right, res);
res.add(root.val);
}
}
```
: O(n) : O(n)
###
,.
![image](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/image.4ypjzygovms0.png)
1 ~ 9.
, , , , , ?
使,
, ,
`, `,
, ,
, ``.
, , 使
,,
![](https://img-blog.csdnimg.cn/20210504155603953.gif)
**: leetcode 102 **
Java Code:
```java
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
//入队 root 节点,也就是第一层
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> list = new ArrayList<>();
int size = queue.size();
for (int i = 0; i < size; ++i) {
TreeNode temp = queue.poll();
//孩子节点不为空,则入队
if (temp.left != null) queue.offer(temp.left);
if (temp.right != null) queue.offer(temp.right);
list.add(temp.val);
}
res.add(list);
}
return res;
}
}
```
C++ Code:
```cpp
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
vector <int> t;
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode * temp = q.front();
q.pop();
if (temp->left != nullptr) q.push(temp->left);
if (temp->right != nullptr) q.push(temp->right);
t.emplace_back(temp->val);
}
res.push_back(t);
}
return res;
}
};
```
Swift Code
```swift
class Solution {
func levelOrder(_ root: TreeNode?) -> [[Int]] {
var res:[[Int]] = []
guard root != nil else {
return res
}
var queue:[TreeNode?] = []
queue.append(root!)
while !queue.isEmpty {
let size = queue.count
var list:[Int] = []
for i in 0..<size {
guard let node = queue.removeFirst() else {
continue
}
if node.left != nil {
queue.append(node.left)
}
if node.right != nil {
queue.append(node.right);
}
list.append(node.val)
}
res.append(list)
}
return res
}
}
```
Go Code:
```go
func levelOrder(root *TreeNode) [][]int {
res := [][]int{}
if root == nil {
return res
}
// 初始化队列时记得把root节点加进去。
que := []*TreeNode{root}
for len(que) != 0 {
t := []int{}
// 这里一定要先求出来que的长度因为在迭代的过程中que的长度是变化的。
l := len(que)
for i := 0; i < l; i++ {
temp := que[0]
que = que[1:]
// 子节点不为空,就入队
if temp.Left != nil { que = append(que, temp.Left) }
if temp.Right != nil { que = append(que, temp.Right) }
t = append(t, temp.Val)
}
res = append(res, t)
}
return res
}
```
On On
- **leetcode 107. II**
- **leetcode 103. 齿**
- **leetcode 199. **
- **leetcode 515. **
- **leetcode 637. **
- **leetcode 116 **
- **leetcode 117 2**