LeetCode 117 Populating Next Right Pointers in Each Node II
2017-08-31 15:08
399 查看
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public void connect(TreeLinkNode root) { if (root == null) { return; } TreeLinkNode mostLeft = root; while (mostLeft != null) { root = mostLeft; while(root != null) { if (root.left != null && root.right != null) { root.left.next = root.right; root.right.next = getNextNode(root.next); } else if (root.left != null || root.right != null) { getNextNode(root).next = getNextNode(root.next); } root = root.next; } mostLeft = getNextNode(mostLeft); } } private TreeLinkNode getNextNode(TreeLinkNode root) { if (root == null) { return null; } if (root.left != null) { return root.left; } if (root.right != null) { return root.right; } return getNextNode(root.next); } }
相关文章推荐
- LeetCode(117) Populating Next Right Pointers in Each Node II
- LeetCode 117: Populating Next Right Pointers in Each Node II
- Leetcode-117: Populating Next Right Pointers in Each Node II
- LeetCode(117) Populating Next Right Pointers in Each Node II
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
- LeetCode 117:Populating Next Right Pointers in Each Node II
- Java for LeetCode 117 Populating Next Right Pointers in Each Node II
- leetcode117~Populating Next Right Pointers in Each Node II
- leetcode 117 —— Populating Next Right Pointers in Each Node II
- leetcode 117 Populating Next Right Pointers in Each Node II
- [leetcode-117]Populating Next Right Pointers in Each Node II(c++)
- leetcode_question_117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode 117 Populating Next Right Pointers in Each Node II (链表 层次遍历 推荐)
- leetcode_117_Populating Next Right Pointers in Each Node II
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]