题目:Given a binary tree, return the postorder traversal of its nodes‘ values.
For example:
Given binary tree {1,#,2,3}, 12/3return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively? 题解:递归方法代码: 1 public void helper(TreeNode root, ArrayList<Integer> re){ 2 if(root==null) 3 return; 4 5 helper(root.left,re); 6 help...
https://leetcode.com/problems/valid-sudoku/Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.A partially filled sudoku which is valid. Note:A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 正好用了一下最...
【057-Insert Interval(插入区间)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2: Given [1,2],[3,5]...
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).” _______6_____...
Copy List with Random PointerA linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.https://leetcode.com/problems/copy-list-with-random-pointer/ 第一把直接暴力两轮遍历。第一轮遍历copy链表,用hash表记录下各个节点,第二乱遍历去赋值链表里的random对象。然后稍稍改进了一下,一次遍历里把能的找到...
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
题解: 这道题就是说给定一个x的值,小于x都放在大于等于x的前面,并且不改变链表之间node原始的相对位置。每次看...
Given n,how many structurally unique BST‘s (binarysearch trees) that store values 1...n?
For example,
Given n = 3, there are a total of 5 unique BST‘s. 1 3 3 2 1\ / / / \ 3 2 1 1 3 2/ / \ 2 1 2 3
给定一个n值,那么从1.2.3....N共N个数,能构建几棵二叉排序树。
解题思路就是:遍...
【024-Swap Nodes in Pairs(成对交换单链表的结点)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 题目大意 给定一...
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.You may assume that each input would have exactly one solution.Input: numbers={2, 7, ...
Problem:
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would haveexactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1.The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).Solution:此题与15题基本类似,甚至更简单一些,只需要比较和...
【114-Flatten Binary Tree to Linked List(二叉树转单链表)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题 Given a binary tree, flatten it to a linked list in-place. For example, Given 1/ 2 5/ \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6题目大意 给定一棵二叉树,将它转成单链表,使用原地算法。 解题思...
题目描述189. 旋转数组给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。示例 1:输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释:向右旋转 1 步: [7,1,2,3,4,5,6]向右旋转 2 步: [6,7,1,2,3,4,5]向右旋转 3 步: [5,6,7,1,2,3,4]示例 2:输入: [-1,-100,3,99] 和 k = 2输出: [3,99,-1,-100]解释: 向右旋转 1 步: [99,-1,-100,3]向右旋转 2 步: [3,99,-1,-100]说明:尽可能想出更多的解决方案,至少有三种不...
题目:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given numberof rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:string convert(string text, int nRows);convert("PAYP...
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given 1->4->3->2->5->2 and x = 3,return 1->2->2->4->3->5. 思路:找出以x为划分的两段链表,并连接在一起,注意维护链表头。 代码:publicclass Solution {public ListNode pa...
Sort a linked list in O(n log n) time using constant space complexity.解题思路:归并排序、快速排序、堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下: public ListNode sortList(ListNode head) {if(head==null||head.next==null)return head;Queue<ListNode> pQueue = new PriorityQueue<ListNode>(0,new Comparator<ListNode>() {//提交的时候不能加初始容量(第一...