This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def constructMaximumBinaryTree(self, nums): | |
""" | |
:type nums: List[int] | |
:rtype: TreeNode | |
""" | |
if len(nums) == 0: | |
return None | |
val = max(nums) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public boolean isSymmetric(TreeNode root) { | |
if (root == null) return true; | |
return helper(root, root); | |
} | |
private boolean helper(TreeNode left, TreeNode right) { | |
return (left != null && right != null && left.val == right.val && helper(left.right, right.left) && helper(left.left, right.right)) || (left == null && right == null); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int[] twoSum(int[] nums, int target) { | |
int[] result = new int[2]; | |
Map<Integer, Integer> visited = new HashMap<>(); | |
for (int i=0; i<nums.length; i++) { | |
if (visited.containsKey(nums[i])) { | |
result[0] = visited.get(nums[i]); | |
result[1] = i; | |
return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public List<List<Integer>> levelOrder(TreeNode root) { | |
List<List<Integer>> res = new LinkedList<List<Integer>>(); | |
Queue<TreeNode> q = new LinkedList<TreeNode>(); | |
if (root == null) return res; | |
q.offer(root); | |
while (!q.isEmpty()) { | |
Integer levelLength = q.size(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int countNodes(TreeNode root) { | |
if (root == null) | |
return 0; | |
int lh = height(root.left); | |
int rh = height(root.right); | |
if (lh == rh) return (1 << lh) + countNodes(root.right); | |
else return (1 << rh) + countNodes(root.left); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public boolean hasPathSum(TreeNode root, int sum) { | |
if (root == null) return false; | |
if (root.left == null && root.right == null) return sum == root.val; | |
sum -= root.val; | |
boolean l = hasPathSum(root.left, sum); | |
boolean r = hasPathSum(root.right, sum); | |
sum += root.val; | |
return l || r; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Codec { | |
private static final String SEP = ","; | |
private static final String NULL = "x"; | |
// Encodes a tree to a single string. | |
public String serialize(TreeNode root) { | |
StringBuilder sb = new StringBuilder(); | |
buildString(root, sb); | |
return sb.toString(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public TreeNode buildTree(int[] preorder, int[] inorder) { | |
return dfs(preorder, inorder, 0, 0, preorder.length); | |
} | |
private TreeNode dfs(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) { | |
if (inStart > inEnd || preStart >= preorder.length) return null; | |
TreeNode node = new TreeNode(preorder[preStart]); | |