public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res = new ArrayList<>(); // Arrays.sort(nums); // not necessary backtrack(res, new ArrayList<>(), nums); return res; }
public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); backtrack(res, new ArrayList<>(), nums, 0); return res; }
public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); //Arrays.sort(nums); 不重复没必要sort backtrack(res, new ArrayList<>(), nums, 0); return res; }
privatevoidbacktrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){ list.add(new ArrayList<>(tempList)); for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, i + 1); tempList.remove(tempList.size() - 1); } }
classSolution{ public Node connect(Node root){ Node dummyHead = new Node(0); // this head will always point to the first element in the current layer we are searching Node pre = dummyHead; // this 'pre' will be the "current node" that builds every single layer Node real_root = root; // just for return statement while(root != null){ if(root.left != null){ pre.next = root.left; pre = pre.next; } if(root.right != null){ pre.next = root.right; pre = pre.next; } root = root.next; if(root == null){ // reach the end of current layer pre = dummyHead; // shift pre back to the beginning, get ready to point to the first element in next layer root = dummyHead.next; ;//root comes down one level below to the first available non null node dummyHead.next = null;//reset dummyhead back to default null } } return real_root; } }
Posted onInLeetcode
,
Tree Symbols count in article: 746Reading time ≈1 mins.
Question
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
Posted onInLeetcode
,
Tree Symbols count in article: 724Reading time ≈1 mins.
Question
Given the root of a binary tree, determine if it is a complete binary tree.
In a complete binary tree, every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.
Posted onInLeetcode
,
Tree Symbols count in article: 673Reading time ≈1 mins.
Question
Given the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-ordertraversal of the binary tree.
publicvoidflatten(TreeNode root){ if (root == null) return; flatten(root.right); flatten(root.left); // linked list only have 1 direction, the first node must be inserted as first root.right = prev; root.left = null; prev = root; } }
Posted onInLeetcode
,
Tree Symbols count in article: 844Reading time ≈1 mins.
Question
Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise.
A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node’s descendants. The tree tree could also be considered as a subtree of itself.
classSolution{ publicbooleanisSubtree(TreeNode s, TreeNode t){ if (s == null && t == null) returntrue; if (s == null) returnfalse; if (isSame(s, t)) returntrue; return isSubtree(s.left, t) || isSubtree(s.right, t); } privatebooleanisSame(TreeNode s, TreeNode t){ if (s == null && t == null) returntrue; if (s == null || t == null) returnfalse; if (s.val != t.val) returnfalse; return isSame(s.left, t.left) && isSame(s.right, t.right); } }
classSolution{ int max = 0; publicintdiameterOfBinaryTree(TreeNode root){ maxDepth(root); return max; } privateintmaxDepth(TreeNode root){ if (root == null) return0; int left = maxDepth(root.left); int right = maxDepth(root.right); max = Math.max(max, left + right); return Math.max(left, right) + 1; } }