LeetCode——有关递归的相关题目

最近发现自己并没有真正理解递归,于是重新把递归的相关题目又重新做了一遍,参考了http://39.96.217.32/blog/4#comment-container讲解,很受用

111. 二叉树的最小深度

这道题有一个陷阱就是当根节点只有左子树或者只有右子树的时候,最小深度是2,而不是1,所以我决定单独判断这两种情况

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    // int left = 0;
    // int right = 0;
    public int minDepth(TreeNode root) {
        if(root == null)
            return 0;
        if(root.left == null&&root.right == null)
            return 1;
        if(root.left == null)
            return minDepth(root.right)+1;
        if(root.right ==null)
            return minDepth(root.left)+1;
        return 1+Math.min(minDepth(root.left),minDepth(root.right));
    }
}

101. 对称二叉树

这道题剑指offer中也有,应该在写一个判断两个树中对应结点,从结构和内容两方面分别判断

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null)
            return true;
        return isSymmetric(root.left,root.right);
    }
    private boolean isSymmetric(TreeNode p,TreeNode q){
        if(p == null&&q == null)
            return true;
        if(p == null||q == null)
            return false;
        if(p.val != q.val)
            return false;
        return isSymmetric(p.left,q.right)&&isSymmetric(p.right,q.left);
    }
}

110. 平衡二叉树

分为两步,首先判断每个节点的最大深度,其次判断每个节点是否平衡,即左右子树只差的绝对值是否小于等于1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null)
            return true;
        return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
    }
    private int maxDepth(TreeNode root){
        if(root == null)
            return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return 1+(left>right?left:right);
    }
}

24. 两两交换链表中的节点

将所有结点分为三部分,两个即将交换的结点,和已经处理完的链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null||head.next == null)
            return head;
        ListNode next = head.next;
        head.next = swapPairs(next.next);
        next.next = head;
        return next;
    }
}

617. 合并二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        TreeNode t = new TreeNode(0);
        if(t1 == null&&t2 ==null)
            return null;
        if(t1 == null)
            return t2;
        if(t2 == null)
            return t1;
        if(t1!= null&&t2 != null)
        {
            t.val = t1.val+t2.val;
            t.left = mergeTrees(t1.left,t2.left);
            t.right = mergeTrees(t1.right,t2.right);
        }
        return t;
    }
}

226. 翻转二叉树

这道题很经典,感觉以后面试会经常出现,重点就是交换每个非叶节点的左右子树,不用想左右子树有一个可能为null的情况

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null)
            return null;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if(root.left != null)
             invertTree(root.left);
        if(root.right != null)
            invertTree(root.right);
        return root;
    }
}

113. 路径总和 II

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List> ans = new ArrayList<>();
    public List> pathSum(TreeNode root, int sum) {
        bfs(root,new ArrayList<>(),sum);
        return ans;
    }
    public void bfs(TreeNode root,List temp,int sum){
        if(root == null)
            return;
        temp.add(root.val);
        int result = sum-root.val;
        if(root.left == null&&root.right == null){
            if(result == 0){
                ans.add(new ArrayList(temp));
            }
        }
        bfs(root.left,temp,result);
        bfs(root.right,temp,result);
        temp.remove(temp.size()-1);
    }
}

112. 路径总和

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root == null)
            return false;
        sum -= root.val;
        if(root.left == null&&root.right == null)
        {
            if(sum == 0)
                return true;
            return false;
        }
        return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
    }
}

 

你可能感兴趣的:(LeetCode)