226. 翻转二叉树
题目
翻转一棵二叉树。
解题思路
- 递归法:递归地交换每个节点的左右子树。终止条件为节点为空。
- 迭代法(使用栈):利用栈来保存节点,通过交换每个节点的左右子树,直到栈为空。
- 标记法:使用栈和标记位,通过
null标记处理递归节点的翻转。
代码
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
TreeNode node = root.left;
root.left = root.right;
root.right = node;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
Stack<TreeNode> st = new Stack<>();
st.push(root);
while(!st.isEmpty()){
TreeNode node = st.peek();
st.pop();
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
if(node.left!=null)st.push(node.left);
if(node.right!=null)st.push(node.right);
}
return root;
}
}
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
Stack<TreeNode> st = new Stack<>();
st.push(root);
while(!st.isEmpty()){
TreeNode node = st.peek();
if(node!=null){
st.pop();
st.push(node);
st.push(null);
if(node.left!=null)st.push(node.left);
if(node.right!=null)st.push(node.right);
}else{
st.pop();
node=st.pop();
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
}
return root;
}
}
101. 对称二叉树
题目
给定一个二叉树,检查它是否是镜像对称的。
解题思路
- 递归法:通过递归比较左右子树是否对称。首先判断左右子树的根节点是否相等,然后分别递归比较左右子树的左子树与右子树、右子树与左子树。
- 迭代法(使用队列):利用队列保存需要成对比较的节点,逐对出队并比较,确保每对节点是对称的。
代码
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)return true;
return compare(root.left,root.right);
}
public boolean compare(TreeNode left,TreeNode right) {
if(left==null && right!=null){
return false;
} else if (left!=null && right==null) {
return false;
}else if (left==null && right==null) {
return true;
}else if (left.val!=right.val) {
return false;
}
boolean outside = compare(left.left,right.right);
boolean insize = compare(left.right,right.left);
boolean result = outside && insize;
return result;
}
}
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)return true;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root.left);
que.offer(root.right);
while(!que.isEmpty()){
TreeNode left=que.poll();
TreeNode right=que.poll();
if (left == null && right == null) {
continue;
}
if(left==null && right!=null){
return false;
} else if (left!=null && right==null) {
return false;
} else if (left.val!=right.val) {
return false;
}
que.offer(left.left);
que.offer(right.right);
que.offer(left.right);
que.offer(right.left);
}
return true;
}
100.相同的树(opens new window)
代码:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q!=null){
return false;
} else if (p != null && q==null) {
return false;
}else if (p == null && q==null) {
return true;
}else if (p.val != q.val) {
return false;
}
boolean one = isSameTree(p.left,q.left);
boolean two = isSameTree(p.right,q.right);
return one && two;
}
}
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null){
return true;
}
Queue<TreeNode> que = new LinkedList<>();
que.offer(p);
que.offer(q);
while(!que.isEmpty()){
TreeNode left = que.poll();
TreeNode right = que.poll();
if (left == null && right == null) {
continue;
}
if(left == null && right!=null){
return false;
} else if (left != null && right==null) {
return false;
}else if (left.val != right.val) {
return false;
}
que.offer(left.left);
que.offer(right.left);
que.offer(left.right);
que.offer(right.right);
}
return true;
}
}
572.另一个树的子树(待解决)
代码:
104. 二叉树的最大深度
题目
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
解题思路
-
后序遍历:
- 使用递归方式遍历二叉树的左右子树,分别得到左右子树的深度。
- 取左右子树深度的较大值,并加 1,即为该节点的深度。
-
前序遍历:
- 使用全局变量记录最大深度,从根节点开始递归计算。
- 遇到非空节点时,记录当前深度,如果比最大深度大,则更新最大深度。
- 遍历左右子树,不断更新最大深度。
代码:
class Solution {
public int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int deep1 = maxDepth(root.left);
int deep2 = maxDepth(root.right);
return 1+Math.max(deep1,deep2);
}
}
前序遍历没看懂
class Solution {
/**
* 递归法(求深度法)
*/
//定义最大深度
int maxnum = 0;
public int maxDepth(TreeNode root) {
ans(root,0);
return maxnum;
}
//递归求解最大深度
void ans(TreeNode tr,int tmp){
if(tr==null) return;
tmp++;
maxnum = maxnum<tmp?tmp:maxnum;
ans(tr.left,tmp);
ans(tr.right,tmp);
tmp--;
}
}
111. 二叉树的最小深度
题目
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
解题思路
- 如果节点为空,直接返回深度为
0。 - 递归计算左子树和右子树的最小深度。
- 若左子树为空而右子树不为空,则返回右子树深度加
1。 - 若右子树为空而左子树不为空,则返回左子树深度加
1。
- 若左子树为空而右子树不为空,则返回右子树深度加
- 当左右子树都不为空时,取左右子树深度的较小值加
1。
代码
class Solution {
public int minDepth(TreeNode root) {
if(root==null)return 0;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
if(root.left==null && root.right!=null)return rightDepth+1;
if(root.left!=null && root.right==null)return leftDepth+1;
return 1+Math.min(leftDepth,rightDepth);
}
}
1151

被折叠的 条评论
为什么被折叠?



