File tree Expand file tree Collapse file tree 3 files changed +69
-23
lines changed
src/main/java/algorithm/leetcode Expand file tree Collapse file tree 3 files changed +69
-23
lines changed Original file line number Diff line number Diff line change 8282| 139 | 动态规划 | [ 单词拆分] ( src/main/java/algorithm/leetcode/Solution139.java ) | 中等 |
8383| 141 | 链表、双指针 | [ 环形链表] ( src/main/java/algorithm/leetcode/Solution141.java ) | 简单 |
8484| 142 | 链表、双指针 | [ 环形链表 II] ( src/main/java/algorithm/leetcode/Solution142.java ) | 中等 |
85+ | 144 | 栈、树 | [ 二叉树的前序遍历] ( src/main/java/algorithm/leetcode/Solution144.java ) | 中等 |
8586| 147 | 排序、链表 | [ 对链表进行插入排序] ( src/main/java/algorithm/leetcode/Solution147.java ) | 中等 |
8687| 148 | 排序、链表 | [ 排序链表] ( src/main/java/algorithm/leetcode/Solution148.java ) | 中等 |
8788| 152 | 数组、动态规划 | [ 乘积最大子序列] ( src/main/java/algorithm/leetcode/Solution152.java ) | 中等 |
Original file line number Diff line number Diff line change 11package algorithm .leetcode ;
22
3+ import java .util .LinkedList ;
4+ import java .util .List ;
5+
36/**
47 * @author: mayuan
5- * @desc:
6- * @date: 2018/08/17
8+ * @desc: 二叉树的前序遍历
9+ * @date: 2019/03/09
710 */
811public class Solution144 {
9- public static void main (String [] args ) {
10-
11- }
12-
13- public boolean hasCycle (ListNode head ) {
14- if (null == head ) {
15- return false ;
12+ public List <Integer > preorderTraversal (TreeNode root ) {
13+ List <Integer > ans = new LinkedList <>();
14+ if (null == root ) {
15+ return ans ;
1616 }
1717
18- ListNode slow = head ;
19- ListNode fast = head . next ;
18+ LinkedList < TreeNode > stack = new LinkedList <>() ;
19+ stack . push ( root ) ;
2020
21- while (null != fast && null != fast .next && null != slow ) {
22- if (slow == fast ) {
23- return true ;
24- }
21+ while (!stack .isEmpty ()) {
22+ TreeNode cur = stack .pop ();
23+ ans .add (cur .val );
2524
26- slow = slow .next ;
27- fast = fast .next .next ;
25+ if (null != cur .right ) {
26+ stack .push (cur .right );
27+ }
28+ if (null != cur .left ) {
29+ stack .push (cur .left );
30+ }
2831 }
29- return false ;
32+
33+ return ans ;
3034 }
3135
32- class ListNode {
36+ public class TreeNode {
3337 int val ;
34- ListNode next ;
38+ TreeNode left ;
39+ TreeNode right ;
3540
36- ListNode (int x ) {
41+ TreeNode (int x ) {
3742 val = x ;
38- next = null ;
3943 }
4044 }
41- }
45+ }
Original file line number Diff line number Diff line change 1+ package algorithm .leetcode ;
2+
3+ /**
4+ * @author: mayuan
5+ * @desc:
6+ * @date: 2018/08/17
7+ */
8+ public class Solution144a {
9+ public static void main (String [] args ) {
10+
11+ }
12+
13+ public boolean hasCycle (ListNode head ) {
14+ if (null == head ) {
15+ return false ;
16+ }
17+
18+ ListNode slow = head ;
19+ ListNode fast = head .next ;
20+
21+ while (null != fast && null != fast .next && null != slow ) {
22+ if (slow == fast ) {
23+ return true ;
24+ }
25+
26+ slow = slow .next ;
27+ fast = fast .next .next ;
28+ }
29+ return false ;
30+ }
31+
32+ class ListNode {
33+ int val ;
34+ ListNode next ;
35+
36+ ListNode (int x ) {
37+ val = x ;
38+ next = null ;
39+ }
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments