1、Sum Root to Leaf Numbers
鏈接:https://leetcode.com/problems/sum-root-to-leaf-numbers/
思路:遞歸,改層的和 = 上層和* 10 +當前val
public int sumNumbers(TreeNode root) {
return sumDFS(root, 0);
}
public int sumDFS(TreeNode root, int sum){
if(root == null)
return 0;
sum = sum * 10 + root.val;
if(root.left == null && root.right == null)
return sum;
return sumDFS(root.left, sum) + sumDFS(root.right, sum);
}
2、Path Sum
鏈接:https://leetcode.com/problems/path-sum/
思路:遞歸
public boolean hasPathSum(TreeNode root, int sum) {
if(root != null && root.val == sum && root.left == null && root.right == null)
return true;
if(root != null){
return hasPathSum(root.left,sum - root.val) || hasPathSum(root.right,sum - root.val);
}
return false;
}
3、Path Sum II
鏈接:https://leetcode.com/problems/path-sum-ii/
思路:採用dfs,注意,需要將list刪除最後一個值。list中保存中間節點。
public void dfs(List<List<Integer>> result,TreeNode root, List<Integer> list, int sum) {
if(root == null)
return ;
if(root.val == sum && root.left == null && root.right == null){
list.add(root.val);
result.add(new ArrayList(list));//注意需要根據list新new一個值,否則傳引用。
list.remove(list.size() - 1);//刪除最後一個葉節點,開始新的遍歷
return;
}
list.add(root.val);
dfs(result,root.left,list, sum - root.val);
dfs(result,root.right,list, sum - root.val);
list.remove(list.size() - 1);
}
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> list = new ArrayList<>();
dfs(result,root, list, sum);
return result;
}
4、Maximum Depth of Binary Tree
鏈接:https://leetcode.com/problems/maximum-depth-of-binary-tree/
思路:遞歸思想
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
5、Minimum Depth of Binary Tree
鏈接:https://leetcode.com/problems/minimum-depth-of-binary-tree/
思路:遞歸,最小深度 = 根節點到葉節點的最小層次
public static int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null) return minDepth(root.right) + 1;
if (root.right == null) return minDepth(root.left) + 1;
return Math.min(minDepth(root.left),minDepth(root.right)) + 1;
}
6、Binary Tree Zigzag Level Order Traversal
鏈接:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
思路:在層次遍歷的基礎上,增加順序
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if(root == null)
return result;
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
boolean flag = false;
while(!queue.isEmpty()){
int size = queue.size();
// List<Integer> list = new ArrayList<>();
LinkedList<Integer> list = new LinkedList<>();
flag = flag == true ? false : true;
for(int i = 0; i < size; i++){
TreeNode tree = queue.poll();
if(flag){
list.add(tree.val);
}else{
list.addFirst(tree.val);
}
if(tree.left != null)
queue.add(tree.left);
if(tree.right != null)
queue.add(tree.right);
}
result.add(list);
}
return result;
}
7、Populating Next Right Pointers in Each Node
鏈接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
思路:層次遍歷變形
public void connect(TreeLinkNode root) {
if(root == null)
return;
Queue<TreeLinkNode> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size - 1; i++){
TreeLinkNode tree = queue.poll();
if(tree.left != null)
queue.add(tree.left);
if(tree.right != null)
queue.add(tree.right);
tree.next = queue.peek();
}
TreeLinkNode tree = queue.poll();
if(tree.left != null)
queue.add(tree.left);
if(tree.right != null)
queue.add(tree.right);
tree.next = null;
}
}
空間複雜度爲O(1)的方法:完全二叉樹中,start指向每層首個節點,cur遍歷該層。
public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode start = root, cur = null;
while(start.left != null){
cur = start;
while(cur != null){
cur.left.next = cur.right;
if(cur.next != null){
cur.right.next = cur.next.left;
}
cur = cur.next;
}
start = start.left;
}
}
8、Populating Next Right Pointers in Each Node II
鏈接:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
思路:不是完全二叉樹,可能缺失左孩子、右孩子。讓p指向有孩子的最左節點,letfmost指向最左節點,cur從最左節點開始遍歷,如果cur是p的左孩子,則判斷是否有右孩子…p後移;如果cur是p的右孩子,p後移;p沒有孩子,p後移;cur的next 爲p的孩子…
public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode leftmost = root;
while(leftmost != null){
TreeLinkNode p = leftmost;
while(p != null && p.left == null && p.right == null)
p = p.next;
if(p == null)
return;
leftmost = p.left != null ? p.left :p.right;
TreeLinkNode cur = leftmost;
while(p != null){
if(cur == p.left){
if(p.right != null){
cur.next = p.right;
cur = cur.next;
}
p = p.next;
}else if(cur == p.right){
p = p.next;
}else {
if(p.left == null && p.right == null){
p = p.next;
continue;
}
cur.next = p.left != null ? p.left : p.right;
cur = cur.next;
}
}
}
}
9、Symmetric Tree
鏈接:https://leetcode.com/problems/symmetric-tree/
思路:判斷是都是對稱樹,比較 n1.val == n2.val ; n1左孩子.val == n2右孩子.val ; n1右孩子.val == n2左孩子.val 依次遞推
public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
return isPalindrome(root.left, root.right);
}
public boolean isPalindrome(TreeNode left,TreeNode right){
if(left == null && right == null)
return true;
if((left != null && right == null) || (left == null && right != null) || (left.val != right.val))
return false;
return isPalindrome(left.left,right.right) && isPalindrome(left.right,right.left) ;
}