Skip to content

Commit 2bd0aeb

Browse files
committed
101.对称二叉树
104.二叉树最大深度 226.翻转二叉树
1 parent b7379a3 commit 2bd0aeb

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
94. 二叉树的中序遍历
2+
101. 对称二叉树
23
102. 二叉树的层序遍历
4+
104. 二叉树的最大深度
35
144. 二叉树的前序遍历
46
145. 二叉树的后序遍历
7+
226. 翻转二叉树
58
589. N叉树的前序遍历

leetcode_Java/Solution0101.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// 对称二叉树
2+
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
20+
21+
/*
22+
* 递归思路:
23+
* 1、方法定义:
24+
* 1)由于要判断两个节点对应的左右子节点值是否相同,原方法入参只有一个节点不够用,需要再定义一个有两个节点作为入参的方法
25+
* 2)不管是原方法还是新方法,其返回类型和返回结果都跟题目要求的返回类型和返回结果一致
26+
* 2、方法功能:
27+
* 1)入参是两个节点,判断两个节点是否相同
28+
* 2)节点都为空、节点都不为空且值相等则返回true;节点只有一个不为空、节点都不为空但值不相等则返回false
29+
* 3、递归逻辑:
30+
* 1)两个节点的左右子节点同样可以调用这个方法,判断节点是否相同,得到true或false的结果
31+
* 2)当前层节点对称,才会继续递归下一层
32+
* 3)每一层,每两个节点作为入参,调用方法,都得到了true或false的结果
33+
* 4)上一层使用下一层的结果,将下一层多个结果进行与运算,得到下一层的节点是否对称
34+
* */
35+
class Solution {
36+
public boolean isSymmetric(TreeNode root) {
37+
if (root == null) {
38+
return true;
39+
}
40+
return isSameTree(root.left, root.right);
41+
}
42+
43+
public boolean isSameTree(TreeNode p, TreeNode q) {
44+
if (p == null && q == null) {
45+
return true;
46+
}
47+
if (p != null && q != null && p.val == q.val) {
48+
boolean l = isSameTree(p.left, q.right);
49+
boolean r = isSameTree(p.right, q.left);
50+
return l && r;
51+
}
52+
return false;
53+
}
54+
}
55+
56+
57+
/*
58+
* 迭代思路:
59+
* 1、定义数据结构:队列按序存放节点
60+
* 2、队列初始化:将根节点的左右节点存入队列
61+
* 3、迭代逻辑:
62+
* 1)遍历队列,弹出两个节点,比较是否相同
63+
* 2)不相同则返回false。都为空、都不为空且值相等,则存入两个节点对应的下一层的四个左右子节点节点
64+
* 3)最终节点都存入了队列,且弹出判断都两两相同,则返回true
65+
* */
66+
class Solution {
67+
public boolean isSymmetric(TreeNode root) {
68+
if (root == null) {
69+
return true;
70+
}
71+
Queue<TreeNode> queue = new LinkedList<>();
72+
queue.offer(root.left);
73+
queue.offer(root.right);
74+
while (!queue.isEmpty()) {
75+
TreeNode p = queue.poll();
76+
TreeNode q = queue.poll();
77+
if (p == null && q == null) {
78+
continue;
79+
}
80+
if (p != null && q != null && p.val == q.val) {
81+
queue.offer(p.left);
82+
queue.offer(q.right);
83+
queue.offer(p.right);
84+
queue.offer(q.left);
85+
continue;
86+
}
87+
return false;
88+
}
89+
return true;
90+
}
91+
}

leetcode_Java/Solution0104.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// 二叉树的最大深度
2+
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
20+
21+
/*
22+
* 递归思路:
23+
* 1、方法功能:入参是一个节点,节点为空返回0,节点不为空返回1
24+
* 2、递归逻辑:
25+
* 1)左右节点同样可以调用这个方法,得到0或1的结果
26+
* 2)每一层,每个节点,调用方法,都得到了0或1的结果
27+
* 3)上一层使用下一层的结果,取下一层左右节点结果的最大值,累加到当前层,从而得到二叉树的最大深度
28+
* */
29+
class Solution {
30+
public int maxDepth(TreeNode root) {
31+
if (root == null) {
32+
return 0;
33+
}
34+
int l = maxDepth(root.left);
35+
int r = maxDepth(root.right);
36+
return 1 + Math.max(l, r);
37+
}
38+
}
39+
40+
41+
/*
42+
* 迭代思路:
43+
* 1、定义数据结构:队列存放每一层的节点
44+
* 2、定义局部变量:deep记录深度;count记录每层节点个数,以便标记队列节点每层结束位置
45+
* 2、实现逻辑:层序遍历,遍历当前层节点时,把下一层节点存入队列,每一层遍历结束后,深度加1,最终得到最大深度
46+
* */
47+
class Solution {
48+
public int maxDepth(TreeNode root) {
49+
if (root == null) {
50+
return 0;
51+
}
52+
int deep = 0;
53+
int count;
54+
Queue<TreeNode> queue = new LinkedList<>();
55+
queue.offer(root);
56+
while (!queue.isEmpty()) {
57+
count = queue.size();
58+
while (count > 0) {
59+
root = queue.poll();
60+
if (root.left != null) {
61+
queue.offer(root.left);
62+
}
63+
if (root.right != null) {
64+
queue.offer(root.right);
65+
}
66+
count--;
67+
}
68+
deep++;
69+
}
70+
return deep;
71+
}
72+
}

leetcode_Java/Solution0226.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// 翻转二叉树
2+
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
20+
21+
/*
22+
* 递归思路:
23+
* 1、方法功能:节点为空时返回空,节点不为空时把节点的左右子节点交换一下
24+
* 2、递归逻辑:
25+
* 1)每一层,每个节点,都需要将其左右子节点交换,就需要调用同个方法
26+
* 2)方法功能是交换节点,不需要接收下一层的返回值给上一层使用
27+
* 3)所有节点都交换完成后,返回根节点
28+
* */
29+
class Solution {
30+
public TreeNode invertTree(TreeNode root) {
31+
if (root == null) {
32+
return null;
33+
}
34+
TreeNode tempNode = root.left;
35+
root.left = root.right;
36+
root.right = tempNode;
37+
invertTree(root.left);
38+
invertTree(root.right);
39+
return root;
40+
}
41+
}
42+
43+
44+
/*
45+
* 迭代思路:
46+
* 1、定义数据结构:栈存放要交换其左右子节点的节点
47+
* 2、保留根节点指针:根节点指针赋值给新节点指针,使用新节点指针遍历节点,最终需要返回根节点指针
48+
* (实例对象存储在堆内存中,对象变量存储在栈中,对象变量就是一个引用,也表示指针,对象变量的赋值就是指针指向的改变)
49+
* 3、栈初始化:将新节点存入栈
50+
* 4、迭代逻辑:
51+
* 1)弹出节点,判断节点的左右子节点是否都为空,为空则跳过
52+
* 2)左右子节点不都为空,则进行节点交换
53+
* 3)然后将不为空的节点存入栈中
54+
* 4)遍历了所有节点且交换了其左右子节点,则翻转二叉树完成,返回根节点
55+
* */
56+
class Solution {
57+
public TreeNode invertTree(TreeNode root) {
58+
if (root == null) {
59+
return null;
60+
}
61+
Stack<TreeNode> stack = new Stack<>();
62+
TreeNode curNode = root;
63+
stack.push(curNode);
64+
while (!stack.isEmpty()) {
65+
curNode = stack.pop();
66+
if (curNode.left == null && curNode.right == null) {
67+
continue;
68+
}
69+
TreeNode tempNode = curNode.left;
70+
curNode.left = curNode.right;
71+
curNode.right = tempNode;
72+
if (curNode.left != null) {
73+
stack.push(curNode.left);
74+
}
75+
if (curNode.right != null) {
76+
stack.push(curNode.right);
77+
}
78+
}
79+
return root;
80+
}
81+
}

0 commit comments

Comments
 (0)