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;
    }
}
相关推荐
Ll13045252981 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展
不想秃头的程序员1 小时前
Vue3 封装 Axios 实战:从基础到生产级,新手也能秒上手
前端·javascript·面试
你听得到112 小时前
我彻底搞懂了 SSE,原来流式响应效果还能这么玩的?(附 JS/Dart 双端实战)
前端·面试·github
晴殇i2 小时前
【前端缓存】localStorage 是同步还是异步的?为什么?
前端·面试
夏鹏今天学习了吗2 小时前
【LeetCode热题100(99/100)】柱状图中最大的矩形
算法·leetcode·职场和发展
千寻girling2 小时前
《 MongoDB 教程 》—— 不可多得的 MongoDB
前端·后端·面试
唐梓航-求职中3 小时前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试
零售ERP菜鸟17 小时前
范式革命:从“信息化”到“数字化”的本质跃迁
大数据·人工智能·职场和发展·创业创新·学习方法·业界资讯
发现一只大呆瓜17 小时前
虚拟列表:支持“向上加载”的历史消息(Vue 3 & React 双版本)
前端·javascript·面试