|
3 | 3 |
|
4 | 4 | import LeetCodeJava.DataStructure.ListNode; |
5 | 5 | import LeetCodeJava.DataStructure.TreeNode; |
| 6 | +import LeetCodeJava.Recursion.PopulatingNextRightPointersInEachNode2; |
6 | 7 | //import jdk.javadoc.internal.doclets.toolkit.util.Utils; |
7 | 8 |
|
8 | 9 | import java.util.*; |
@@ -2592,4 +2593,115 @@ private String updateStringWithIdx(String input, String newStr, int idx){ |
2592 | 2593 | return sb.replace(idx, idx+1, newStr).toString(); |
2593 | 2594 | } |
2594 | 2595 |
|
| 2596 | + // LC 116 |
| 2597 | + /** |
| 2598 | + * You are given a perfect binary tree where all leaves are on the same level, |
| 2599 | + * and every parent has two children. The binary tree has the following definition: |
| 2600 | + * |
| 2601 | + * |
| 2602 | + * |
| 2603 | + * Populate each next pointer to point to its next right node. |
| 2604 | + * If there is no next right node, the next pointer should be set to NULL. |
| 2605 | + * |
| 2606 | + * Initially, all next pointers are set to NULL. |
| 2607 | + * |
| 2608 | + * |
| 2609 | + * Exp 1) |
| 2610 | + * |
| 2611 | + * Input: {"$id":"1", |
| 2612 | + * "left":{"$id":"2","left":{"$id":"3","left":null,"next":null, |
| 2613 | + * "right":null,"val":4},"next":null, |
| 2614 | + * "right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2}, |
| 2615 | + * "next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1} |
| 2616 | + * |
| 2617 | + * Output: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1} |
| 2618 | + * |
| 2619 | + * Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. |
| 2620 | + * |
| 2621 | + * |
| 2622 | + * IDEA: RECURSION (? |
| 2623 | + * |
| 2624 | + * |
| 2625 | + */ |
| 2626 | +// class Node { |
| 2627 | +// public int val; |
| 2628 | +// public workspace6.Node left; |
| 2629 | +// public workspace6.Node right; |
| 2630 | +// public workspace6.Node next; |
| 2631 | +// |
| 2632 | +// public Node() {} |
| 2633 | +// |
| 2634 | +// public Node(int _val) { |
| 2635 | +// val = _val; |
| 2636 | +// } |
| 2637 | +// |
| 2638 | +// public Node(int _val, workspace6.Node _left, workspace6.Node _right, workspace6.Node _next) { |
| 2639 | +// val = _val; |
| 2640 | +// left = _left; |
| 2641 | +// right = _right; |
| 2642 | +// next = _next; |
| 2643 | +// } |
| 2644 | +// }; |
| 2645 | +// public Node connect(Node root) { |
| 2646 | +// |
| 2647 | +// return null; |
| 2648 | +// } |
| 2649 | + |
| 2650 | + class Node { |
| 2651 | + public int val; |
| 2652 | + public Node left; |
| 2653 | + public Node right; |
| 2654 | + public Node next; |
| 2655 | + |
| 2656 | + public Node() {} |
| 2657 | + |
| 2658 | + public Node(int _val) { |
| 2659 | + val = _val; |
| 2660 | + } |
| 2661 | + |
| 2662 | + public Node(int _val, Node _left, Node _right, Node _next) { |
| 2663 | + val = _val; |
| 2664 | + left = _left; |
| 2665 | + right = _right; |
| 2666 | + next = _next; |
| 2667 | + } |
| 2668 | + } |
| 2669 | + |
| 2670 | + public class Solution { |
| 2671 | + public Node connect(Node root) { |
| 2672 | + if (root == null) { |
| 2673 | + return null; |
| 2674 | + } |
| 2675 | + |
| 2676 | + // Start with the root node |
| 2677 | + Node currentLevel = root; |
| 2678 | + |
| 2679 | + while (currentLevel != null) { |
| 2680 | + // Traverse through the current level |
| 2681 | + Node temp = currentLevel; |
| 2682 | + while (temp != null) { |
| 2683 | + if (temp.left != null) { |
| 2684 | + // Connect the left child to the right child |
| 2685 | + temp.left.next = temp.right; |
| 2686 | + } |
| 2687 | + |
| 2688 | + if (temp.right != null && temp.next != null) { |
| 2689 | + // Connect the right child to the next node's left child |
| 2690 | + temp.right.next = temp.next.left; |
| 2691 | + } |
| 2692 | + |
| 2693 | + // Move to the next node in the current level |
| 2694 | + temp = temp.next; |
| 2695 | + } |
| 2696 | + |
| 2697 | + // Move to the next level (leftmost node of the next level) |
| 2698 | + currentLevel = currentLevel.left; |
| 2699 | + } |
| 2700 | + |
| 2701 | + return root; |
| 2702 | + } |
| 2703 | + } |
| 2704 | + |
| 2705 | + |
| 2706 | + |
2595 | 2707 | } |
0 commit comments