GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-3/problems/src/tree/NextRightPointerII.java at master · abcdvemm/leetcode-3 Expand file tree/ Copy pathNextRightPointerII.javaMore file actions 63 lines (58 loc) · 1.63 KB / Copy pathNextRightPointerII.javaFile metadata and controls63 lines (58 loc) · 1.63 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 package tree; /** * Created by gouthamvidyapradhan on 03/10/2017. * * <p>Follow up for problem "Populating Next Right Pointers in Each Node". * * <p>What if the given tree could be any binary tree? Would your previous solution still work? * * <p>Note: * * <p>You may only use constant extra space. For example, Given the following binary tree, 1 / \ 2 3 * / \ \ 4 5 7 After calling your function, the tree should look like: 1 -> NULL / \ 2 -> 3 -> NULL * / \ \ 4-> 5 -> 7 -> NULL */ public class NextRightPointerII { public static void main(String[] args) throws Exception { TreeLinkNode root = new TreeLinkNode(1); root.left = new TreeLinkNode(2); root.right = new TreeLinkNode(3); root.right.right = new TreeLinkNode(4); root.right.right.left = new TreeLinkNode(5); root.right.right.right = new TreeLinkNode(6); new NextRightPointerII().connect(root); } public void connect(TreeLinkNode root) { TreeLinkNode prev = new TreeLinkNode(0); TreeLinkNode first = null; while (root != null) { if (root.left != null) { prev.next = root.left; prev = root.left; if (first == null) { first = root.left; } } if (root.right != null) { prev.next = root.right; prev = root.right; if (first == null) { first = root.right; } } root = root.next; if (root == null) { root = first; first = null; prev = new TreeLinkNode(0); } } } public static class TreeLinkNode { int val; TreeLinkNode left, right, next; TreeLinkNode(int x) { val = x; } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |