Skip to content

Commit aec7244

Browse files
author
yennj12
committed
update 116 java
1 parent 6c4193f commit aec7244

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

leetcode_java/src/main/java/LeetCodeJava/Recursion/PopulatingNextRightPointersInEachNode.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,120 @@
4141
*/
4242
public class PopulatingNextRightPointersInEachNode {
4343

44+
class Node {
45+
public int val;
46+
public Node left;
47+
public Node right;
48+
public Node next;
49+
50+
public Node() {}
51+
52+
public Node(int _val) {
53+
val = _val;
54+
}
55+
56+
public Node(int _val, Node _left, Node _right, Node _next) {
57+
val = _val;
58+
left = _left;
59+
right = _right;
60+
next = _next;
61+
}
62+
}
63+
4464
// V0
4565
// public Node connect(Node root) {
4666
//
4767
// }
4868

4969
// V1
70+
// IDEA: RECURSION (gpt)
71+
/**
72+
*
73+
* In this problem, we are tasked with populating the next pointer of each node in a perfect binary tree to point to its next right node. If there is no next right node, the next pointer should be set to null.
74+
*
75+
* 1) Key Observations:
76+
*
77+
* - Perfect Binary Tree: A perfect binary tree means that every parent has exactly two children and all leaf nodes are at the same level.
78+
* - Constant Extra Space: We are asked to use constant extra space. This means we cannot use additional data structures like queues, lists, or hashmaps.
79+
* - Level-order traversal: We can achieve the desired result by using level-by-level traversal to connect each node to its adjacent node in the same level.
80+
*
81+
*
82+
* 2) Solution Approach:
83+
*
84+
* We can solve this using a level-by-level approach, where we iterate over each level and connect the next pointers:
85+
*
86+
* step 1) Start at the root:
87+
* The root has no left or right siblings, so it has no next pointer to populate.
88+
*
89+
* step 2) Iterate through each level:
90+
* Starting from the root, for each level, connect the left child’s
91+
* next to the right child, and if possible, connect the right child’s
92+
* next to the next node’s left child.
93+
*
94+
* step 3)
95+
* Link nodes on the same level: Since it’s a perfect binary tree,
96+
* every level is fully filled. So, the left child of a node always
97+
* has a sibling (right child), and we can set their next pointers accordingly.
98+
*
99+
* step 4)
100+
* Use only constant space: We don’t use any extra data structures.
101+
* Instead, we leverage the tree’s structure, using next pointers to move across
102+
* the tree level by level.
103+
*
104+
*
105+
* 3) Explanation:
106+
*
107+
* Initial check: If the root is null, we return null immediately as there’s no tree to process.
108+
*
109+
* Outer while loop: We start at the root and move down level by level. currentLevel represents the leftmost node of the current level.
110+
*
111+
* Inner while loop: We process each node at the current level. For each node:
112+
*
113+
* We connect the left child to the right child by setting temp.left.next = temp.right.
114+
* If there is a next node (temp.next), we connect the right child of the current node to the left child of the next node by setting temp.right.next = temp.next.left.
115+
* Move to next level: After processing all nodes at the current level, we move to the leftmost node of the next level by setting currentLevel = currentLevel.left.
116+
*
117+
* Return the root: The tree is now updated with the correct next pointers, and we return the root node.
118+
*
119+
* 5) Performance
120+
*
121+
* - Time Complexity: O(n) where n is the number of nodes in the tree. We traverse each node exactly once.
122+
* - Space Complexity: O(1). Since we are not using any extra space apart from the input tree and a few constant variables, the space complexity is constant.
123+
*
124+
*/
125+
public Node connect_1(Node root) {
126+
if (root == null) {
127+
return null;
128+
}
129+
130+
// Start with the root node
131+
Node currentLevel = root;
132+
133+
while (currentLevel != null) {
134+
// Traverse through the current level
135+
Node temp = currentLevel;
136+
while (temp != null) {
137+
if (temp.left != null) {
138+
// Connect the left child to the right child
139+
temp.left.next = temp.right;
140+
}
141+
142+
if (temp.right != null && temp.next != null) {
143+
// Connect the right child to the next node's left child
144+
temp.right.next = temp.next.left;
145+
}
146+
147+
// Move to the next node in the current level
148+
temp = temp.next;
149+
}
150+
151+
// Move to the next level (leftmost node of the next level)
152+
currentLevel = currentLevel.left;
153+
}
154+
155+
return root;
156+
}
157+
158+
50159
// V2
51160
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import LeetCodeJava.DataStructure.ListNode;
55
import LeetCodeJava.DataStructure.TreeNode;
6+
import LeetCodeJava.Recursion.PopulatingNextRightPointersInEachNode2;
67
//import jdk.javadoc.internal.doclets.toolkit.util.Utils;
78

89
import java.util.*;
@@ -2592,4 +2593,115 @@ private String updateStringWithIdx(String input, String newStr, int idx){
25922593
return sb.replace(idx, idx+1, newStr).toString();
25932594
}
25942595

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+
25952707
}

0 commit comments

Comments
 (0)