[Algorithm design and analysis (answer after class)] List/Tree recursion

1

For the singly linked list L without a leading node, a recursive algorithm is designed to output all the node values ​​in a positive order.

public class Solution1 {
    
    
    public void printL(ListNode node) {
    
    
        if (node == null) {
    
    
            return;
        }
        System.out.println(node.val);
        printL(node.next);
    }
}

2

For the singly linked list L without a leading node, a recursive algorithm is designed to output all node values ​​in reverse order.

public class Solution2 {
    
    
    public void printL(ListNode node) {
    
    
        if (node == null) {
    
    
            return;
        }
        printL(node.next);
        System.out.println(node.val);
    }
}

3

For a non-empty singly linked list L without a leading node, design a recursive algorithm to return the address of the largest node (assuming that such a node is unique).

public class Solution3 {
    
    
    public ListNode findMax(ListNode node) {
    
    
        if (node.next == null) {
    
    
            return node;
        }
        return node.val > findMax(node.next).val ? node : findMax(node.next);
    }
}

4

For a singly linked list L without a leading node, design a recursive algorithm to return the address of the first node with the value x, and return null if there is no such node.

public class Solution4 {
    
    
    public ListNode findNode(ListNode node, int target) {
    
    
        if (node == null) {
    
    
            return null;
        }
        if (node.val == target) {
    
    
            return node;
        }
        return findNode(node.next, target);
    }
}

5

For the singly linked list L without a leading node, a recursive algorithm is designed to delete the first node whose value is x.

public class Solution5 {
    
    

    /**
     * 虽说是递归,本质还是双/三指针的向右滑动
     * 设置一个dummy头结点很有必要
     */
    public void deleteNode(ListNode head, int target) {
    
    
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        delete(dummy, head, target);
    }


    private void delete(ListNode pre, ListNode node, int target) {
    
    
        if (node == null) {
    
    
            return;
        }
        if (node.val == target) {
    
    
            pre.next = node.next;
            return;
        }
        delete(node, node.next, target);
    }
}

6

Assuming that the binary tree is stored in a binary chain storage structure and the node value is of type int, a recursive algorithm is designed to find the sum of all leaf nodes in the binary tree bt.

public class Solution6 {
    
    
    public int nodeSum(TreeNode node) {
    
    
        if (node == null) {
    
    
            return 0;
        }
        if (node.left == null && node.right == null) {
    
    
            return node.val;
        }
        return nodeSum(node.left) + nodeSum(node.right);
    }
}

7

Assuming that the binary tree is stored in a binary chain storage structure, and the node value is of type int, a recursive algorithm is designed to find the number of nodes in the binary tree bt whose value is greater than or equal to k.

public class Solution7 {
    
    
    public int countNode(TreeNode node, int k) {
    
    
        if (node == null) {
    
    
            return 0;
        }
        return (node.val >= k ? 1 : 0) + countNode(node.left, k) + countNode(node.right, k);
    }
}

8

Assuming that the binary tree is stored in a binary chain storage structure, and all node values ​​are different, design a node level with a recursive algorithm value of x (with node level 1), and return 0 if no such node is found.

public class Solution8 {
    
    
    public int findLayerOfK(TreeNode node, int k, int level) {
    
    
        if (node == null) {
    
    
            return 0;
        }
        if (node.val == k) {
    
    
            return level;
        }
        int t1 = findLayerOfK(node.left, k, level + 1);
        int t2 = findLayerOfK(node.right, k, level + 1);
        return t1 != 0 ? t1 : t2;
    }
}

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

⭐️ This must be the most beautiful Java solution in the entire network>_<

Guess you like

Origin blog.csdn.net/m0_46202073/article/details/115040965