day44(12.25)——leetcode面试经典150

100. 相同的树

100. 相同的树

这几题都比较好写,心情愉悦

题目:

题解:

java 复制代码
/**
 * 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 flag = true;
    public void simple(TreeNode p, TreeNode q) {
        if(flag == false) {
            return;
        }
        if((p == null && q != null) || (p != null && q == null)) {
            flag = false;
            return;
        }
        else if(p == null && q == null) {
            return;
        }
        else if(p.val != q.val) {
            flag = false;
            return;
        }
        simple(p.left,q.left);
        simple(p.right,q.right);

    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        simple(p,q);
        return flag;
    }
}

226. 翻转二叉树

226. 翻转二叉树

题目:

题解:

java 复制代码
/**
 * 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 void dfs(TreeNode root) {
        if(root == null) {
            return ;
        }
        TreeNode t = root.left;
        root.left = root.right;
        root.right = t;
        dfs(root.left);
        dfs(root.right); 
    } 

    public TreeNode invertTree(TreeNode root) {
        dfs(root);
        return root;
    }
}
相关推荐
程序员爱钓鱼1 小时前
Go 开发环境安装(Windows、macOS、Linux)
后端·面试·go
触底反弹2 小时前
🤯 面试被问 AI Workflow 和 Agent 有啥区别?3 张图 + 2 段代码讲清楚!
人工智能·设计模式·面试
uzong8 小时前
一个好的面试官,应该让候选人感到被尊重
面试
触底反弹12 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
沉默王二13 小时前
腾讯一面,我霸气反问:“你说你们在做Agent项目,说说 SubAgent、Plan 模式、Skill 调用这些你们都是怎么做的?”面试官一直在擦汗。。
面试·agent·ai编程
程序员Rock17 小时前
上位机开发-MODBUS面试常见问题
c语言·c++·面试·职场和发展·上位机
PBitW21 小时前
git 中容易遗忘的点 (二) ⚡⚡⚡
前端·git·面试
浩哥学JavaAI21 小时前
2026年最新AI agent面试(07)_大模型架构基础
人工智能·面试·架构
PBitW21 小时前
git 中容易遗忘的点 (三) 🚀🚀🚀
前端·git·面试
十八岁牛爷爷1 天前
C/C++ 六大关键字(static、const、volatile、extern、register、inline)面试三层级详解
开发语言·c++·面试