摘要:
Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?[解题思路]递归解法 1 public ArrayList preorderTraversal(TreeNode root) { 2 // IMPORTANT: ... 阅读全文
摘要:
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?[解题思路]双指针问题 详见:链表 相交结点与环问题 1 public class Solution { 2 public ListNode detectCycle(ListNode head) { 3 // IMPORTANT: Please reset any member d... 阅读全文
摘要:
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?[解题思路]双指针问题,fast指针每次走两步,slow指针每次走一步,如果fast指针可以"追上"slow指针,则说明存在环 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 // IMPORTANT: Please reset any member data yo.. 阅读全文