add
This commit is contained in:
parent
5c1401ba7a
commit
bb6fcf1c53
35
5.leetcode/src/com/fanxb/common/Q112.java
Normal file
35
5.leetcode/src/com/fanxb/common/Q112.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Q112 {
|
||||||
|
private static class Node {
|
||||||
|
public int val;
|
||||||
|
public Node left;
|
||||||
|
public Node right;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPathSum(TreeNode root, int targetSum) {
|
||||||
|
if (root == null) return false;
|
||||||
|
return hasPathSum(root, targetSum, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPathSum(TreeNode root, int targetSum, int curSum) {
|
||||||
|
if (root == null) return false;
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
return targetSum == curSum + root.val;
|
||||||
|
}
|
||||||
|
int sum = root.val + curSum;
|
||||||
|
if (sum > targetSum) return false;
|
||||||
|
return hasPathSum(root.left, targetSum, sum) || hasPathSum(root.right, targetSum, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
52
5.leetcode/src/com/fanxb/common/Q114.java
Normal file
52
5.leetcode/src/com/fanxb/common/Q114.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class Q114 {
|
||||||
|
private static class Node {
|
||||||
|
public int val;
|
||||||
|
public Node left;
|
||||||
|
public Node right;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void flatten(TreeNode root) {
|
||||||
|
if (root == null) return;
|
||||||
|
LinkedList<TreeNode> nodes = new LinkedList<>();
|
||||||
|
read(root, nodes);
|
||||||
|
for (int i = 0; i < nodes.size() - 1; i++) {
|
||||||
|
nodes.get(i).left = null;
|
||||||
|
nodes.get(i).right = nodes.get(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read(TreeNode root, LinkedList<TreeNode> nodes) {
|
||||||
|
if (root == null) return;
|
||||||
|
else nodes.add(root);
|
||||||
|
read(root.left, nodes);
|
||||||
|
read(root.right, nodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void flatten1(TreeNode root) {
|
||||||
|
if (root == null) return;
|
||||||
|
if (root.left != null) {
|
||||||
|
TreeNode right = root.right;
|
||||||
|
root.right = root.left;
|
||||||
|
root.left = null;
|
||||||
|
TreeNode endRight = root.right;
|
||||||
|
while (endRight.right != null) {
|
||||||
|
endRight = endRight.right;
|
||||||
|
}
|
||||||
|
endRight.right = right;
|
||||||
|
}
|
||||||
|
flatten1(root.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
66
5.leetcode/src/com/fanxb/common/Q117.java
Normal file
66
5.leetcode/src/com/fanxb/common/Q117.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Q117 {
|
||||||
|
private static class Node {
|
||||||
|
public int val;
|
||||||
|
public Node left;
|
||||||
|
public Node right;
|
||||||
|
public Node next;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val, Node _left, Node _right, Node _next) {
|
||||||
|
val = _val;
|
||||||
|
left = _left;
|
||||||
|
right = _right;
|
||||||
|
next = _next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node connect(Node root) {
|
||||||
|
if (root == null) return null;
|
||||||
|
LinkedList<Node> list = new LinkedList<>();
|
||||||
|
list.addFirst(root);
|
||||||
|
while (!list.isEmpty()) {
|
||||||
|
int size = list.size();
|
||||||
|
while (size-- > 0) {
|
||||||
|
Node node = list.removeLast();
|
||||||
|
if (size != 0) node.next = list.peekLast();
|
||||||
|
if (node.left != null) list.addFirst(node.left);
|
||||||
|
if (node.right != null) list.addFirst(node.right);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node connect1(Node root) {
|
||||||
|
if (root == null) return null;
|
||||||
|
Node cur = root;
|
||||||
|
while (cur != null) {
|
||||||
|
Node temp = cur, start = new Node(), end = start;
|
||||||
|
while (temp != null) {
|
||||||
|
if (temp.left != null) {
|
||||||
|
end.next = temp.left;
|
||||||
|
end = end.next;
|
||||||
|
}
|
||||||
|
if (temp.right != null) {
|
||||||
|
end.next = temp.right;
|
||||||
|
end = end.next;
|
||||||
|
}
|
||||||
|
temp = temp.next;
|
||||||
|
}
|
||||||
|
cur = start.next;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
33
5.leetcode/src/com/fanxb/common/Q129.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q129.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package com.fanxb.common;
|
||||||
|
|
||||||
|
public class Q129 {
|
||||||
|
private static class Node {
|
||||||
|
public int val;
|
||||||
|
public Node left;
|
||||||
|
public Node right;
|
||||||
|
|
||||||
|
public Node() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int res = 0;
|
||||||
|
|
||||||
|
public int sumNumbers(TreeNode root) {
|
||||||
|
deal(root, 0);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deal(TreeNode root, int cur) {
|
||||||
|
if (root == null) return;
|
||||||
|
cur = cur * 10 + root.val;
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
res += cur;
|
||||||
|
}
|
||||||
|
deal(root.left, cur);
|
||||||
|
deal(root.right, cur);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user