二叉树集合
1.二叉树的中序遍历
链接:94. 二叉树的中序遍历 - 力扣(LeetCode)
代码:
法1:递归
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
class Solution { List<Integer> ans;
public List<Integer> inorderTraversal(TreeNode root) { ans = new ArrayList<>();
dfs(root);
return ans; }
private void dfs(TreeNode root) { if (root == null) { return; }
dfs(root.left);
ans.add(root.val);
dfs(root.right); } }
|
法2:迭代
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
class Solution { public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<Integer>();
Deque<TreeNode> stk = new LinkedList<TreeNode>();
while (root != null || !stk.isEmpty()) {
while (root != null) { stk.push(root); root = root.left; }
root = stk.pop();
ans.add(root.val);
root = root.right; }
return ans; } }
|
法3:Morris中序遍历
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| ```
## 2.二叉树的最大深度
**链接:**[104. 二叉树的最大深度 - 力扣(LeetCode)](https:
**代码**:
法1:DFS
```java
class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } }
|
法2:BFS
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; }
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int ans = 0;
while (!q.isEmpty()) { int sz = q.size();
while (sz-- > 0) { TreeNode cur = q.poll();
if (cur.left != null) { q.offer(cur.left); }
if (cur.right != null) { q.offer(cur.right); } }
ans++; }
return ans; } }
|
3.翻转二叉树
链接:226. 翻转二叉树 - 力扣(LeetCode)
代码:
法1:递归
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return null; TreeNode tmp = root.left; root.left = invertTree(root.right); root.right = invertTree(tmp); return root; } }
|
法2:迭代
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) { return null; }
Deque<TreeNode> stk = new LinkedList<>();
stk.push(root);
while (!stk.isEmpty()) { TreeNode cur = stk.pop();
TreeNode tmp = cur.left; cur.left = cur.right; cur.right = tmp;
if (cur.left != null) { stk.push(cur.left); }
if (cur.right != null) { stk.push(cur.right); } }
return root; } }
|
4.对称二叉树
链接:101. 对称二叉树 - 力扣(LeetCode)
代码:
法1:递归
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public boolean isSymmetric(TreeNode root) { return root == null || recur(root.left, root.right); }
boolean recur(TreeNode L, TreeNode R) { if (L == null && R == null) { return true; }
if (L == null || R == null || L.val != R.val) { return false; }
return recur(L.left, R.right) && recur(L.right, R.left); } }
|
5.二叉树的直径
链接:543. 二叉树的直径 - 力扣(LeetCode)
代码:
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Solution { private int ans;
public int diameterOfBinaryTree(TreeNode root) { dfs(root); return ans; }
private int dfs(TreeNode node) { if (node == null) { return -1; } int lLen = dfs(node.left) + 1; int rLen = dfs(node.right) + 1; ans = Math.max(ans, lLen + rLen); return Math.max(lLen, rLen); } }
|
6.二叉树的层序遍历
链接:102. 二叉树的层序遍历 - 力扣(LeetCode)
代码:
纯正的bfs
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ans = new ArrayList<>();
if (root == null) { return ans; }
Deque<TreeNode> q = new LinkedList<>(); q.offer(root);
while (!q.isEmpty()) { List<Integer> curList = new ArrayList<>();
int sz = q.size();
while (sz-- > 0) { TreeNode cur = q.poll();
curList.add(cur.val);
if (cur.left != null) { q.offer(cur.left); }
if (cur.right != null) { q.offer(cur.right); } }
ans.add(curList); }
return ans; } }
|
7.将有序数组转换为二叉搜索树
链接:108. 将有序数组转换为二叉搜索树 - 力扣(LeetCode)
代码:
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| class Solution { public TreeNode sortedArrayToBST(int[] nums) { return dfs(nums, 0, nums.length - 1); }
private TreeNode dfs(int[] nums, int l, int r) { if (l > r) { return null; }
int mid = l + (r - l) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = dfs(nums, l, mid - 1);
node.right = dfs(nums, mid + 1, r);
return node; } }
|
8.验证二叉树
链接:98. 验证二叉搜索树 - 力扣(LeetCode)
代码:
中序遍历即可
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
class Solution { TreeNode pre = null;
public boolean isValidBST(TreeNode root) { if (root == null) { return true; }
return dfs(root); }
private boolean dfs(TreeNode root) { if (root == null) { return true; }
if (!dfs(root.left)) { return false; }
if (pre != null && pre.val >= root.val) { return false; }
pre = root;
return dfs(root.right); } }
|
9.二叉搜索树中第k小的树
链接:230. 二叉搜索树中第 K 小的元素 - 力扣(LeetCode)
代码:
法1:中序遍历第k个
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| class Solution { int ans = -1; int count = 0;
private void dfs(TreeNode root, int k) { if (root == null || ans != -1) { return; }
dfs(root.left, k);
count++; if (count == k) { ans = root.val; return; }
dfs(root.right, k); }
public int kthSmallest(TreeNode root, int k) { dfs(root, k); return ans; } }
|
10.二叉树的右视图
链接:199. 二叉树的右视图 - 力扣(LeetCode)
代码:
法1:BFS取最后一个即可(或改变添加顺序取第一个)
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| class Solution {
public List<Integer> rightSideView(TreeNode root) { if (root == null) { return new ArrayList<>(); }
List<Integer> ans = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while (q.size() > 0) { int sz = q.size();
boolean vis = true;
while (sz-- > 0) { TreeNode cur = q.poll();
if (vis) { ans.add(cur.val); }
vis = false;
if (cur.right != null) { q.add(cur.right); }
if (cur.left != null) { q.add(cur.left); } } }
return ans; } }
|
11.二叉树展开为链表
链接:114. 二叉树展开为链表 - 力扣(LeetCode)
代码:
法1:
Java
评论区
欢迎留下你的想法评论系统还没有接入配置,界面已经预留好了。