Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5e77a5

Browse files
author
applewjg
committed
add
Change-Id: I17be190edfb9a08b3f55110302a588b6f363544b
1 parent7502e20 commita5e77a5

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

‎BinaryTreeLevelOrderTraversalII.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Author:King, wangjingui@outlook.com
2+
Author:Andy, nkuwjg@gmail.com
33
Date: Dec 12, 2014
44
Problem: Binary Tree Level Order Traversal II
55
Difficulty: easy
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 12, 2015
4+
Problem: Binary Tree Zigzag Level Order Traversal
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
7+
Notes:
8+
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left
9+
to right, then right to left for the next level and alternate between).
10+
For example:
11+
Given binary tree {3,9,20,#,#,15,7},
12+
3
13+
/ \
14+
9 20
15+
/ \
16+
15 7
17+
return its zigzag level order traversal as:
18+
[
19+
[3],
20+
[20,9],
21+
[15,7]
22+
]
23+
24+
Solution: 1. Queue + reverse.
25+
2. Two stacks.
26+
*/
27+
28+
/**
29+
* Definition for binary tree
30+
* public class TreeNode {
31+
* int val;
32+
* TreeNode left;
33+
* TreeNode right;
34+
* TreeNode(int x) { val = x; }
35+
* }
36+
*/
37+
publicclassSolution {
38+
publicList<List<Integer>>zigzagLevelOrder_1(TreeNoderoot) {
39+
List<List<Integer>>res =newArrayList<List<Integer>>();
40+
if (root ==null)returnres;
41+
Queue<TreeNode>q =newLinkedList<TreeNode>();
42+
q.offer(root);
43+
q.offer(null);
44+
List<Integer>level =newArrayList<Integer>();
45+
intdepth =0;
46+
while(true) {
47+
TreeNodenode =q.poll();
48+
if (node !=null) {
49+
level.add(node.val);
50+
if(node.left!=null)q.offer(node.left);
51+
if(node.right!=null)q.offer(node.right);
52+
}else {
53+
if (depth %2 ==1)Collections.reverse(level);
54+
res.add(level);
55+
depth++;
56+
level =newArrayList<Integer>();
57+
if(q.isEmpty()==true)break;
58+
q.offer(null);
59+
}
60+
}
61+
returnres;
62+
}
63+
publicList<List<Integer>>zigzagLevelOrder(TreeNoderoot) {
64+
List<List<Integer>>res =newArrayList<List<Integer>>();
65+
if (root ==null)returnres;
66+
Stack<TreeNode>cur =newStack<TreeNode>();
67+
Stack<TreeNode>last =newStack<TreeNode>();
68+
booleanleft2right =true;
69+
last.push(root);
70+
List<Integer>level =newArrayList<Integer>();
71+
while (last.empty() ==false) {
72+
TreeNodenode =last.pop();
73+
if (node !=null) {
74+
level.add(node.val);
75+
if (left2right) {
76+
if(node.left!=null)cur.push(node.left);
77+
if(node.right!=null)cur.push(node.right);
78+
}else {
79+
if(node.right!=null)cur.push(node.right);
80+
if(node.left!=null)cur.push(node.left);
81+
}
82+
}
83+
if (last.empty() ==true) {
84+
if (level.size() !=0)
85+
res.add(level);
86+
level =newArrayList<Integer>();
87+
Stack<TreeNode>temp =last;
88+
last =cur;
89+
cur =temp;
90+
left2right = !left2right;
91+
}
92+
}
93+
returnres;
94+
}
95+
}

‎LengthofLastWord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Author:King, wangjingui@outlook.com
2+
Author:Andy, nkuwjg@gmail.com
33
Date: Dec 25, 2014
44
Problem: Length of Last Word
55
Difficulty: Easy

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp