Given a binary tree, return the preorder traversal of its nodes’ values.
Example:
1 2 3 4 5 6 7 8 Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3]
Follow up: Recursive solution is trivial, could you do it iteratively?
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 class Solution { public List<Integer> preorderTraversal (TreeNode root) { List<Integer> res = new ArrayList<>(); if (root == null ) return res; Stack<TreeNode> stack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { TreeNode cur = stack.pop(); if (cur.right != null ) stack.push(cur.right); if (cur.left != null ) stack.push(cur.left); res.add(cur.val); } return res; } }
Given a binary tree, return the inorder traversal of its nodes’ values.
Example:
1 2 3 4 5 6 7 8 Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import java.util.List;class Solution { public List<Integer> inorderTraversal (TreeNode root) { List<Integer> res = new ArrayList<>(); if (root == null ) return res; Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; while (cur != null || !stack.isEmpty()) { while (cur != null ) { stack.push(cur); cur = cur.left; } cur = stack.pop(); res.add(cur.val); cur = cur.right; } return res; } }
Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
1 2 3 4 5 6 7 8 Input: [1,null,2,3] 1 \ 2 / 3 Output: [3,2,1]
Follow up: Recursive solution is trivial, could you do it iteratively?
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import java.util.LinkedList;import java.util.Stack;class Solution { public List<Integer> postorderTraversal (TreeNode root) { LinkedList<Integer> res = new LinkedList<>(); if (root == null ) return res; Stack<TreeNode> stack = new Stack<>(); stack.push(root); while (!stack.isEmpty()) { TreeNode cur = stack.pop(); res.addFirst(cur.val); if (cur.left != null ) stack.push(cur.left); if (cur.right != null ) stack.push(cur.right); } return res; } }
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]
,
return its level order traversal as:
1 2 3 4 5 [ [3], [9,20], [15,7] ]
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null ) return res; helper(res, root, 0 ); return res; } public static void helper (List<List<Integer>> res, TreeNode root, int level) { if (root == null ) return ; if (level >= res.size()) { res.add(new ArrayList<>()); } res.get(level).add(root.val); helper(res, root.left, level + 1 ); helper(res, root.right, level + 1 ); } }
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example: Given binary tree [3,9,20,null,null,15,7]
,
return its bottom-up level order traversal as:
1 2 3 4 5 [ [15,7], [9,20], [3] ]
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null ) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); List<Integer> list = new ArrayList<>(); for (int i = 0 ; i < size; i++) { TreeNode cur = queue.poll(); if (cur.left != null ) queue.offer(cur.left); if (cur.right != null ) queue.offer(cur.right); list.add(cur.val); } res.add(0 , list); } return res; } }
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example: Given binary tree [3,9,20,null,null,15,7]
,
return its zigzag level order traversal as:
1 2 3 4 5 [ [3], [20,9], [15,7] ]
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 30 31 32 33 34 35 class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null ) return res; Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); boolean x = true ; while (!queue.isEmpty()) { int size = queue.size(); List<Integer> list = new ArrayList<>(); for (int i = 0 ; i < size; i++) { TreeNode cur = queue.poll(); if (x) { list.add(cur.val); } else { list.add(0 , cur.val); } if (cur.left != null ) queue.offer(cur.left); if (cur.right != null ) queue.offer(cur.right); } res.add(list); x = x ? false : true ; } return res; } }
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
1 2 3 Input: [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
1 2 3 Input: [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public List<String> summaryRanges (int [] nums) { List<String> res = new ArrayList<>(); if (nums == null || nums.length == 0 ) return res; for (int i = 0 ; i < nums.length; i++) { int num = nums[i]; while (i < nums.length - 1 && nums[i] + 1 == nums[i + 1 ]) { i++; } if (num != nums[i]) { res.add(num + "->" + nums[i]); } else { res.add(num + "" ); } } return res; } }
The string "PAYPALISHIRING"
is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
1 2 3 P A H N A P L S I I G Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
1 string convert(string s, int numRows);
Example 1:
1 2 Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Example 2:
1 2 3 4 5 6 7 8 Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation: P I N A L S I G Y A H R P I
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public String convert (String s, int numRows) { if (numRows <= 1 ) return s; StringBuilder[] sb = new StringBuilder[numRows]; for (int i = 0 ; i < sb.length; i++) { sb[i] = new StringBuilder("" ); } for (int i = 0 ; i < s.length(); i++) { int index = i % (2 * numRows - 2 ); index = index < numRows ? index : 2 * numRows - 2 - index; sb[index].append(s.charAt(i)); } for (int i = 1 ; i < sb.length; i++) { sb[0 ].append(sb[i]); } return sb[0 ].toString(); } }
Given a list of non negative integers, arrange them such that they form the largest number.
Example 1:
1 2 Input: [10,2] Output: "210"
Example 2:
1 2 Input: [3,30,34,5,9] Output: "9534330"
Note: The result may be very large, so you need to return a string instead of an integer.
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 class Solution { public String largestNumber (int [] nums) { if (nums == null || nums.length == 0 ) return "" ; String[] res = new String[nums.length]; for (int i = 0 ; i < nums.length; i++) { res[i] = String.valueOf(nums[i]); } Arrays.sort(res, new Comparator<String>(){ @Override public int compare (String str1, String str2) { String s1 = str1 + str2; String s2 = str2 + str1; return s2.compareTo(s1); } }); if (res[0 ].charAt(0 ) == '0' ) { return "0" ; } StringBuilder sb = new StringBuilder(); for (String s : res) { sb.append(s); } return sb.toString(); } }
Write a program that outputs the string representation of numbers from 1 to n .
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public List<String> fizzBuzz (int n) { List<String> res = new ArrayList<>(); if (n == 0 ) return res; for (int i = 1 ; i <= n; i++) { if (i % 3 == 0 && i % 5 == 0 ) { res.add("FizzBuzz" ); } else if (i % 3 == 0 ) { res.add("Fizz" ); } else if (i % 5 == 0 ) { res.add("Buzz" ); } else { res.add(String.valueOf(i)); } } return res; } }