Hello friends! What I am sharing with you today is the second article about the binary tree leetcode brushing questions, let's take a look together.
Article Directory
1. Symmetric binary tree
Symmetric binary tree of leetcode (difficulty: easy)
Topic requirements
Given the root node root of a binary tree, check if it is axisymmetric.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
}
}
example
Input: root = [1,2,2,3,4,4,3]
Output: true
Question ideas
For this question, we need to first judge whether the structure of the left subtree of the binary tree is the same as that of the right subtree. If they are different, it means asymmetry. If they are the same, return false if they are not the same. If they are the same, continue to check whether the right tree of the left subtree is the same as the left tree of the right subtree. Because the method given by the title has only one parameter, we need to write a method with two parameters. The method we write is the main body of the judgment. The method given by the title is only used to receive the return value of the method we wrote.
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
//当根节点为null时我们规定返回true
if(root == null) {
return true;
}
if(isSymmetricChild(root.left,root.right)) {
return true;
}
return false;
}
public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
//判断左右子树结构是否相同
if(leftTree == null || rightTree == null) {
if(leftTree == null && rightTree == null) {
return true;
}else {
return false;
}
}
if(leftTree.val != rightTree.val) {
return false;
}
return isSymmetricChild(leftTree.left,rightTree.right)
&& isSymmetricChild(leftTree.right,rightTree.left);
}
}
2. The maximum depth of the binary tree
The maximum depth of the binary tree of leetcode (difficulty: easy)
Topic requirements
Given a binary tree, find its maximum depth.
The depth of a binary tree is the number of nodes on the longest path from the root node to the furthest leaf node.
Explanation: A leaf node refers to a node without child nodes.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
}
}
example
Given a binary tree [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
returns its maximum depth of 3.
Question ideas
The maximum depth of a binary tree refers to the larger of the depth of the left subtree and the depth of the right subtree. Let's simplify the problem first.
Return 0 when the root is null, and then continue the previous recursion.
Return the greater value of the depth of the left subtree and the depth of the right subtree of root + 1, because the depth of the left subtree and the right subtree are both 0, so return 1
The same is true for the right subtree here, returning 1
returns 1+1+1 = 3
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
//当root为null时返回0
if(root == null) {
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
//返回leftDepth和rightDepth中深度较大的
return leftDepth>rightDepth?(leftDepth+1):(rightDepth+1);
}
}
3. Flip the binary tree
Leetcode flipping binary tree (difficulty: easy)
Topic requirements
Given a binary tree root node, flip the binary tree and return its root node.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
}
example
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Question ideas
We need to exchange the left tree of the left subtree of the binary tree with the right tree of the right subtree, and exchange the right tree of the left subtree with the left tree of the right subtree.
The root goes down, until it stops when the root is null, first walks the left tree, then the right tree, and finally the root.
Return null if root is null, and continue to complete the previous recursion
Exchange the left and right subtrees of root, because the left and right subtrees of root are null here, so there is no exchange
Here is the same process as before
.
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) {
return null;
}
invertTree(root.left);
invertTree(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}
4. Balanced binary tree
Leetcode's balanced binary tree (difficulty: easy)
Topic requirements
Given a binary tree, determine whether it is a height-balanced binary tree.
In this question, a height balanced binary tree is defined as:
The absolute value of the height difference between the left and right subtrees of each node in a binary tree does not exceed 1.
example
Input: root = [3,9,20,null,null,15,7]
Output: true
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
input: root = []
output: true
Question ideas
In fact, if we want to solve this problem, we only need to make a slight modification to the previous maximum depth of the binary tree. We don't just return the subtrees with greater depth in the left and right trees, but judge whether the absolute value of the difference between the depths of the left and right subtrees is greater than 1. If it is greater than 1, we return -1, and if it is less than or equal to 1, we return the subtrees with the deeper depth The depth of the large subtree.
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
//当root为null时返回0,即使刚进来时root为null,也可以考虑到
if(root == null) {
return 0;
}
int leftTree = maxDepth(root.left);
//我们在这里直接先进行判断,如果不符合就直接返回,可以减少时间的消耗
if(leftTree < 0) {
return -1;
}
int rightTree = maxDepth(root.right);
if(rightTree < 0) {
return -1;
}
if(Math.abs(leftTree-rightTree) <= 1) {
return Math.max(leftTree,rightTree)+1;
}else {
return -1;
}
}
public boolean isBalanced(TreeNode root) {
if(maxDepth(root) < 0) {
return false;
}
return true;
}
}