Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions xdu/passer/Java/passer_102.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.passer._0320;
/*
Problem:Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values.
(ie: from left to right, level by level).
For example:
Given binary tree
[3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
*/

import java.util.ArrayList;
import java.util.List;

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}

public class Solution {
List<List<Integer>> listAll = new ArrayList<>();

public List<List<Integer>> levelOrder(TreeNode root) {
levelOrder(root, 0);
return listAll;
}

/**
* @param root node of tree
* @param depth depth of tree
*/
public void levelOrder(TreeNode root, int depth) {
if (root == null)
return;
List<Integer> list = null;
if (depth == listAll.size()) {
list = new ArrayList<>();
listAll.add(list);
} else {
list = listAll.get(depth);
}
list.add(root.val);
levelOrder(root.left, depth + 1);
levelOrder(root.right, depth + 1);
}
}
43 changes: 43 additions & 0 deletions xdu/passer/Java/passer_114.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.passer._0321;

/*
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
*/
public class Flatten {
public void flatten(TreeNode root) {
helper(root);
}

private TreeNode helper(TreeNode root) {
if (root == null)
return null;
TreeNode ans = helper(root.left);
root.left = helper(root.right);
root.right = ans;
ans = root;
while (ans.right != null) {
ans = ans.right;
}
ans.right = root.left;
root.left = null;
return root;
}
}
29 changes: 29 additions & 0 deletions xdu/passer/Java/passer_124.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.passer._0321;

class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int x) {
val = x;
}
}

public class MaxSumOfPath {
int maxValue;

public int maxPathSum(TreeNode root) {
maxValue = Integer.MIN_VALUE;
maxPathDown(root);
return maxValue;
}

private int maxPathDown(TreeNode node) {
if (node == null) return 0;
int left = Math.max(0, maxPathDown(node.left));
int right = Math.max(0, maxPathDown(node.right));
maxValue = Math.max(maxValue, left + right + node.val);
return Math.max(left, right) + node.val;
}
}
26 changes: 26 additions & 0 deletions xdu/passer/Java/passer_136.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.passer._0321;

/*Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]
Output: 1
Example 2:

Input: [4,1,2,1,2]
Output: 4
*/
public class SingleNum {
public int singleNumber(int[] nums) {
int ret = 0;
for (int i = 0; i < nums.length; i++) {
ret ^= nums[i];
}
return ret;
}
}