class Solution {
public List < List < Integer > > levelOrder ( TreeNode root) {
Queue < TreeNode > queue = new LinkedList < > ( ) ;
List < List < Integer > > result = new ArrayList < > ( ) ;
if ( root == null ) return result;
queue. offer ( root) ;
while ( ! queue. isEmpty ( ) ) {
int leng = queue. size ( ) ;
List < Integer > path = new ArrayList < > ( ) ;
for ( int i = 0 ; i < leng; i++ ) {
TreeNode cur = queue. poll ( ) ;
path. add ( cur. val) ;
if ( cur. left != null ) queue. offer ( cur. left) ;
if ( cur. right != null ) queue. offer ( cur. right) ;
}
result. add ( new ArrayList ( path) ) ;
}
return result;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
104 二叉树的最大深度
递归,之前做过 ,可直接在原函数上递归,优化终止条件
时间复杂度O(n),空间复杂度O(|二叉树高度|)
class Solution {
public int maxDepth ( TreeNode root) {
if ( root == null ) return 0 ;
return countDepth ( root, 1 ) ;
}
private int countDepth ( TreeNode root, int depth) {
if ( root. left == null && root. right == null ) return depth;
int left = root. left == null ? depth : countDepth ( root. left, depth + 1 ) ;
int right = root. right == null ? depth : countDepth ( root. right, depth + 1 ) ;
return Math . max ( left, right) ;
}
}
class Solution {
public int maxDepth ( TreeNode root) {
if ( root == null ) {
return 0 ;
}
int left = maxDepth ( root. left) ;
int right = maxDepth ( root. right) ;
return Math . max ( left, right) + 1 ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
105 从前序与中序遍历 序列构造二叉树
class Solution {
public TreeNode buildTree ( int [ ] preorder, int [ ] inorder) {
return buildTree1 ( preorder, 0 , preorder. length - 1 , inorder, 0 , inorder. length - 1 ) ;
}
private TreeNode buildTree1 ( int [ ] preorder, int preLeft, int preRight, int [ ] inorder, int inLeft, int inRight) {
if ( preRight < preLeft || inRight < inLeft) return null ;
TreeNode root = new TreeNode ( preorder[ preLeft] ) ;
int inMid = inLeft;
for ( int i = inLeft; i <= inRight; i++ ) {
if ( inorder[ i] == preorder[ preLeft] ) inMid = i;
}
int preMid = preLeft + inMid - inLeft;
root. left = buildTree1 ( preorder, preLeft + 1 , preMid, inorder, inLeft, inMid - 1 ) ;
root. right = buildTree1 ( preorder, preMid + 1 , preRight, inorder, inMid + 1 , inRight) ;
return root;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
114 二叉树展开为链表
递归,前序遍历时使用数组保存节点顺序,最后统一修改节点左右指针
class Solution {
public void flatten ( TreeNode root) {
List < TreeNode > preList = new ArrayList < > ( ) ;
preorder ( root, preList) ;
TreeNode pre = root;
for ( int i = 1 ; i < preList. size ( ) ; i++ ) {
TreeNode cur = preList. get ( i) ;
pre. left = null ;
pre. right = cur;
pre = cur;
}
}
private void preorder ( TreeNode root, List < TreeNode > preList) {
if ( root == null ) return ;
preList. add ( root) ;
preorder ( root. left, preList) ;
preorder ( root. right, preList) ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class Solution {
public int maxProfit ( int [ ] prices) {
int max = 0 ;
int cur = 0 ;
for ( int i = 1 ; i < prices. length; i++ ) {
if ( prices[ i] < prices[ cur] ) cur = i;
max = Math . max ( max, prices[ i] - prices[ cur] ) ;
}
return max;
}
}
124 二叉树中的最大路径和
递归,计算每个节点的最大贡献值,即该节点的值和左右节点的值的最大和,如果贡献为正,则记录最大路径和,否则不记录。
class Solution {
int max = Integer . MIN_VALUE;
public int maxPathSum ( TreeNode root) {
getMax ( root) ;
return max;
}
private int getMax ( TreeNode root) {
if ( root == null ) return 0 ;
int left = Math . max ( getMax ( root. left) , 0 ) ;
int right = Math . max ( getMax ( root. right) , 0 ) ;
int cur = root. val + left + right;
max = Math . max ( max, cur) ;
return root. val + Math . max ( left, right) ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
128 最长连续序列
哈希表遍历数组并存储到set中,遍历set,若当前元素-1不在set中,说明当前元素为第一个,不断更新当前长度和最大长度;若在set中,说明当前元素不为第一个,跳过(由第一个元素进行遍历)。
class Solution {
public int longestConsecutive ( int [ ] nums) {
Set < Integer > set = new HashSet < > ( ) ;
for ( int num : nums) {
set. add ( num) ;
}
int max = 0 ;
for ( int num : set) {
if ( set. contains ( num - 1 ) ) continue ;
int cur = 0 ;
while ( set. contains ( num) ) {
cur++ ;
num++ ;
}
max = Math . max ( max, cur) ;
}
return max;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
136 只出现一次的数字
class Solution {
public int singleNumber ( int [ ] nums) {
int res = 0 ;
for ( int num : nums) {
res ^= num;
}
return res;
}
}
139 单词拆分
完全背包,之前做过 ,可以多次使用,遍历字符串,判断当前位置之前的字符串是否能够拼接,因此同时遍历当前字符串的分割位置。遍历顺序从小到大。
class Solution {
public boolean wordBreak ( String s, List < String > wordDict) {
boolean [ ] dp = new boolean [ s. length ( ) + 1 ] ;
dp[ 0 ] = true ;
for ( int i = 1 ; i <= s. length ( ) ; i++ ) {
for ( int j = 0 ; j < i; j++ ) {
if ( wordDict. contains ( s. substring ( j, i) ) && dp[ j] ) {
dp[ i] = true ;
break ;
}
}
}
return dp[ s. length ( ) ] ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
141 环形链表
public class Solution {
public boolean hasCycle ( ListNode head) {
ListNode fast = head;
ListNode slow = head;
while ( fast != null && fast. next != null ) {
fast = fast. next. next;
slow = slow. next;
if ( fast == slow) return true ;
}
return false ;
}
}