236.二叉树的最近公共祖先 | 234.回文链表 | 739.每日温度 | 226.反转二叉树

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //终止条件
        if(root == null || root == p || root == q){
            return root;
        }
        //单层逻辑
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

        if(left != null && right == null){
            return left;
        }else if(left == null && right != null){
            return right;
        }else if(left == null && right == null){
            return null;
        }else{
            return root;
        }
    }
}
class Solution {
    public boolean isPalindrome(ListNode head) {
        //链表反转 虚拟头节点
        ListNode reverse = reverseList(head);
        //对比
        while(head != null){
            if(head.val != reverse.val){
                return false;
            }
            reverse = reverse.next;
            head = head.next;
        }
        return true;
    }

    private ListNode reverseList (ListNode head){
        ListNode prev = null;
        
        while(head != null){
            ListNode next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
}
//两层for
class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int len = temperatures.length;
        boolean found = false;
        int[] res = new int[len];
        for(int i = 0;i<len;i++){
            int count = 0;
            for(int j = i+1;j<len;j++){
                count++;
                if(temperatures[j] > temperatures[i]){
                    res[i] = count;
                    found = true;
                    break;
                }
            }
        if (!found) {
                res[i] = 0;
            }
        }
        return res;
    }
}
//栈

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        Stack<Integer> stack = new Stack<>();
        int len = temperatures.length;
        int[] res = new int[len];

        for(int i = 0; i<len; i++){
            while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
                int ind = stack.pop();
                res[ind] = i - ind;
                
            }
            stack.push(i);
        }
        return res;

    }
}
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return null;
        }
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);

        
        root.right = left;
        root.left = right;

        return root;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值