标签归档:leetcode

RSS feed of leetcode

[LeetCode题解]从两个有序数组的并集中寻找第k小元素

Given two sorted arrays A, B of size m and n respectively. Find the k-th smallest element in the union of A and B. You can assume that there are no duplicate elements.

不得不承认这道题目解决起来非常的巧妙。像大多数难题一样,需要经过非常巧妙的观察才可以用简洁的方式求解。

朴素解法, O(m+n ...

继续阅读

[LeetCode]Reorder List

题目描述:

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes' values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

题目大意:

给定一个单链表L:L0→L1→…→Ln-1→Ln
重新将其排序为 ...

继续阅读

[LeetCode]Binary Tree Preorder Traversal

题目描述:

Given a binary tree, return the preorder traversal of its nodes' values.

For example:

Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,2,3].

题目大意:

给定一棵二叉树,返回节点值的先序遍历结果(尽量使用非递归算法)。

解题思路:

使用数据结构栈(Stack)

先序遍历非递归算法的伪代码如下(摘自Wikipedia):

iterativePreorder(node)
  parentStack ...

继续阅读