摘要: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) ... 阅读全文
posted @ 2013-05-12 21:36 infinityu 阅读(194) 评论(0) 推荐(0) 编辑
摘要: Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode 阅读全文
posted @ 2013-05-12 17:16 infinityu 阅读(183) 评论(0) 推荐(0) 编辑
摘要: Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.Note:Givenm,nsatisfy the following condition:1 ?m?n? length of list.解法:需要定位待翻转部分的头与尾,以及翻转部分相邻的两个节点,同时还要考虑m和n之间的关系。 1 /** 2 阅读全文
posted @ 2013-05-12 17:05 infinityu 阅读(561) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6Hints:If you notice carefully ... 阅读全文
posted @ 2013-05-12 16:09 infinityu 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).Note:Bonus points if you could solve it both recursively and iteratively.解法 1:非递归解法使用双端队列dequeue记录对称的节点,依次从dequeue的头尾取出两个节点进行判断,两个节点的值以及节点左右孩子结构分别相同的情况下,将他们的孩子按顺序加入dequeue。再依次从dequeue的头尾取出两个节点进行判断,直到dequeue 阅读全文
posted @ 2013-05-12 14:50 infinityu 阅读(207) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater thanthe node's key.Both the left and ri 阅读全文
posted @ 2013-05-12 14:20 infinityu 阅读(255) 评论(0) 推荐(0) 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解法:递归求解,思路同上一篇博客中用先序和中序构造二叉树类似。 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 ... 阅读全文
posted @ 2013-05-12 13:54 infinityu 阅读(200) 评论(0) 推荐(0) 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.解法:递归求解。 基本情况:先序与中序数组长度小于1时,返回NULL。 递归步骤:首先确定preorder的第一个元素一定是该树的root,再在inorder中找到该元素,标记为index,index左部为左子树的中序,右部为右子树的中序;随后通过左右子树中序的长度(可能为0),在preorder中确定左右子树的先序。 1 .. 阅读全文
posted @ 2013-05-12 13:38 infinityu 阅读(557) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return theinordertraversal of its nodes' values.Note:Recursive solution is trivial, could you do it iteratively?二叉树中序遍历,非递归解法。使用栈记录中序遍历时中间节点的访问顺序,从栈中弹出的顺序即为中序。Program Runtime:16 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * Tr... 阅读全文
posted @ 2013-05-12 12:53 infinityu 阅读(152) 评论(0) 推荐(0) 编辑